#C2650. Largest Surrounded Square
Largest Surrounded Square
Largest Surrounded Square
Given a binary matrix with dimensions (n \times m), find the size of the largest square submatrix that is filled entirely with 1s and is completely surrounded by 0s. The border surrounding the square (i.e., the cells immediately above, below, to the left, and to the right of the square, including the corner-adjacent cells) must consist solely of 0s. Note that the square must be strictly inside the matrix so that a full border exists. If no such square exists, output 0.
Formally, if the square has side length (s) and its top-left corner is at ((i, j)) (with 0-indexing), then the square covers cells ({(i, j), (i, j+1), \ldots, (i+s-1, j+s-1)}) and the surrounding border are the cells at positions:
({(i-1, j-1), \ldots, (i-1, j+s)}) (top border),
({(i+s, j-1), \ldots, (i+s, j+s)}) (bottom border),
({(i, j-1), \ldots, (i+s-1, j-1)}) (left border), and
({(i, j+s), \ldots, (i+s-1, j+s)}) (right border).
You are to read the matrix from standard input and output the maximum (s) that satisfies these conditions.
inputFormat
The first line contains two integers (n) and (m) ((n, m \ge 1)). The next (n) lines each contain (m) space-separated integers (each either 0 or 1) representing the matrix.
outputFormat
Output a single integer representing the size of the largest square (i.e., the length of its side) that meets the given conditions. If no valid square exists, output 0.## sample
5 5
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0
3