Rock is a robot travelling inside a $N \times M$ grid, where the top-left corner is $(0, 0)$ and the bottom-right corner is $(N - 1, M - 1)$. We say two squares are adjacent if they share an edge. It takes the robot a second to walk to any of the (up to 4) adjacent squares. The robot cannot walk out of the grid.
Rock has little battery remaining, so it needs to find a replacement battery immediately. Luckily, there may be some batteries placed on the grid. How long will it take Rock to reach the nearest battery?
Input
The first line of input contains two integer $N$ and $M$.
The next $N$ lines contain the layout of the grid.
Rock's position is marked by an X
, and each battery's location is marked by a B
.
All other squares are marked by a .
.
Output
Output the number of seconds it will take Rock to reach the nearest battery.
If there are no batteries on the grid, output oh no
.
Constraints
$2 \le N, M \le 1000$
Sample Test Cases
Input | Output | |
---|---|---|
5 5 .B... ..... ...X. B.... ..B.. |
3 |
Scoring: Per Subtask
Authored by s16f22