Task
You are given a matrix with rows and columns filled with three types of values:
- Empty squares, marked with .
- Walls, marked with #
- Characters, marked with
The task is simple: find the maximum value of such that if we expand the walls by squares in all directions, all characters marked with can still reach each other. It is guaranteed that the characters can reach each other initially.
A character can move on the matrix in the four directions (up, down, left, right).
An expansion by consists of the following operation: For each square in the initial matrix marked with #, we will mark all squares at a distance of at most from it with #. In other words, all squares at a distance of at most from a wall in the initial matrix also become walls.
If a character is blocked by a wall due to the expansion by , then we cannot expand the wall by .
It is guaranteed that there is at least one wall in each test and at least two characters.
Input data
The first line of the input file expansion.in
contains two integers, and .
The next lines will contain the description of the matrix, which has rows and columns and contains the characters mentioned above, with representing the square on row and column .
Output data
The first line of the output file expansion.out
will contain a single integer, the value of , as described in the statement.
Constraints and clarifications
- ;
# | Score | Constraints |
---|---|---|
1 | 12 | = # for any and |
2 | 45 | |
3 | 43 | No other constraints |
Example 1
expansion.in
5 5
R...R
.....
..#..
.....
R...R
expansion.out
1
Explanation
In red, we marked the initial walls, and in orange, the walls obtained as a result of the expansions.
For the first example, all s can reach each other if we expand the walls by , but this will be impossible if we expand the walls by .
Example 2
expansion.in
2 6
R#.#.R
......
expansion.out
0
Explanation
For the second example, if we expand the walls by square, the at () would be trapped between walls.