Skip to content
UPEEC

Signal Spread

Hard#graphs#bfs#multi-source-bfs#matrix· solved by 0 · time limit 2.0 s

A grid of sensor nodes: S already has the signal, o is a node without it, . is empty. Every second, each node with the signal passes it to its up/down/left/right neighbours. After how many seconds do all nodes have the signal? Print -1 if some node never gets it.

Input

RR and CC, then RR lines of CC characters.

Output

The number of seconds, or -1.

Constraints

1≤R,C≤10001 \le R, C \le 1000

Sample 1

Input

3 3
Soo
oo.
.oo

Output

4

Sample 2

Input

3 3
Soo
.oo
o.o

Output

-1

Sign in or join free to run and submit your solution.