#C771. Largest Empty Square Subgrid

    ID: 51611 Type: Default 1000ms 256MiB

Largest Empty Square Subgrid

Largest Empty Square Subgrid

You are given an n × m grid where each cell is either empty or contains an obstacle. An empty cell is denoted by 0 and an obstacle by 1. Your task is to compute the side length of the largest square subgrid that contains only empty cells.

One effective way to solve this problem is by using dynamic programming. Let \(dp[i][j]\) represent the side length of the largest square ending at cell \((i,j)\) (with \(i\) and \(j\) being 0-indexed). Then, if the current cell is empty, the value can be computed as:

$$dp[i][j] = \min\{ dp[i-1][j],\; dp[i][j-1],\; dp[i-1][j-1] \} + 1 $$

If the cell contains an obstacle, \(dp[i][j] = 0\). The answer to the problem is the maximum value found in the \(dp\) table.

inputFormat

The input is given via stdin and has the following format:

  1. The first line contains two integers \(n\) and \(m\): the number of rows and columns of the grid.
  2. The next \(n\) lines each contain \(m\) space-separated integers (either 0 or 1) representing one row of the grid.

outputFormat

Output a single integer to stdout, which is the side length of the largest square subgrid containing only empty cells.

## sample
5 5
1 0 1 0 0
1 0 0 1 0
1 0 0 0 0
0 1 0 0 1
0 0 0 1 0
2

</p>