#B3980. Building Size Estimation

    ID: 11637 Type: Default 1000ms 256MiB

Building Size Estimation

Building Size Estimation

Dr. X's spy satellite photos contain exactly one complete square with its boundary pixels marked in white and all other pixels marked in black. In the photo, each pixel represents a $1\times1$ meter square. The white pixels (denoted by 1) form the boundary of a square building.

Your task is to determine the side length (in meters) of the square. You can assume that the white pixels are contiguous and form a perfect square boundary. Mathematically, if the topmost white pixel is at row $r_{min}$, the bottommost at row $r_{max}$, the leftmost at column $c_{min}$, and the rightmost at column $c_{max}$, then the side length of the square is given by:

$$L = c_{max} - c_{min} + 1 = r_{max} - r_{min} + 1.$$

Note that the interior of the square (if any) may not be filled with white pixels.

inputFormat

The first line contains two integers $n$ and $m$ ($1 \leq n, m \leq 1000$), representing the number of rows and columns of the photo, respectively. Following this, there are $n$ lines, each containing $m$ space-separated integers (either 0 or 1), representing the pixels of the photo.

outputFormat

Output a single integer representing the side length (in meters) of the square building.

sample

4 4
0 0 0 0
0 1 1 0
0 1 1 0
0 0 0 0
2