#K10891. Largest Enclosed Square
Largest Enclosed Square
Largest Enclosed Square
You are given an integer n and an n x n
grid of characters. Each cell is either a '#' which represents a building or a '.' which represents an empty space. A square block of buildings is defined as a contiguous square sub-grid consisting entirely of '#' characters. The square is considered enclosed if there is a complete border of empty spaces ('.') immediately surrounding the square. Formally, if the square is defined by its top-left corner (i, j) with side length k, then:
- For every cell on the top border (row i-1, columns j to j+k-1) and the bottom border (row i+k, columns j to j+k-1), the cell must exist and be a dot ('.').
- For every cell on the left border (column j-1, rows i to i+k-1) and right border (column j+k, rows i to i+k-1), the cell must exist and be a dot ('.').
Note that the building block itself must lie entirely within the grid such that its enclosing border exists. Your task is to determine the largest integer k (side length) for which there exists an enclosed square block of buildings in the grid. If no such square exists, output 0.
Input/Output Specifications:
inputFormat
The input is given via standard input with the following format:
n row1 row2 ... rown
where the first line contains an integer n (the size of the grid), and each of the next n lines contains a string of length n representing a row of the grid.
outputFormat
Output a single integer representing the side length of the largest enclosed square block of buildings. If no valid enclosed square exists, output 0.
## sample5
.....
.###.
.###.
.###.
.....
3