expansion

Time limit: 2s Memory limit: 64MB Input: expansion.in Output: expansion.out

Task

You are given a matrix with nn rows and mm columns filled with three types of values:

  1. Empty squares, marked with .
  2. Walls, marked with #
  3. Characters, marked with RR

The task is simple: find the maximum value of kk such that if we expand the walls by kk squares in all directions, all characters marked with RR 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 kk 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 kk from it with #. In other words, all squares at a distance of at most kk from a wall in the initial matrix also become walls.

If a character is blocked by a wall due to the expansion by xx, then we cannot expand the wall by xx.

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, nn and mm.

The next nn lines will contain the description of the matrix, which has nn rows and mm columns and contains the characters mentioned above, with ai,ja_{i, j} representing the square on row ii and column jj.

Output data

The first line of the output file expansion.out will contain a single integer, the value of kk, as described in the statement.

Constraints and clarifications

  • 1n,m21031 \leq n, m \leq 2 \cdot 10^3;
# Score Constraints
1 12 ai,ja_{i, j} = # for any 2in12 \leq i \leq n-1 and 2jm12 \leq j \leq m-1
2 45 1n,m1001 \leq n, m \leq 100
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 RRs can reach each other if we expand the walls by 11, but this will be impossible if we expand the walls by 22.

Example 2

expansion.in

2 6
R#.#.R
......

expansion.out

0

Explanation

For the second example, if we expand the walls by 11 square, the RR at (1,11, 1) would be trapped between walls.

Log in or sign up to be able to send submissions!