#C2219. Largest Square Sub-matrix Area

    ID: 45511 Type: Default 1000ms 256MiB

Largest Square Sub-matrix Area

Largest Square Sub-matrix Area

Given an n × m binary matrix consisting of 0s and 1s, your task is to find the area of the largest square sub-matrix that contains only 1s.

The challenge can be solved using dynamic programming. For each cell \(dp[i][j]\) representing the maximum side length of a square with bottom-right corner at cell \( (i, j) \), the recurrence relation is:

\( dp[i][j] = \min(dp[i-1][j],\; dp[i][j-1],\; dp[i-1][j-1]) + 1 \)

If the current cell contains 0, then \(dp[i][j]=0\). The final answer is the square of the maximum value in the \(dp\) table.

Note: The input is given through standard input (stdin) and the result must be printed to standard output (stdout).

inputFormat

The input begins with a line containing two integers, n and m, representing the number of rows and columns respectively.

This is followed by n lines, each containing m integers (either 0 or 1) separated by spaces.

For example:

5 6
0 1 1 0 1 1
1 1 1 1 0 0
1 1 1 1 1 1
0 1 1 1 1 1
1 0 1 1 1 1

outputFormat

Output a single integer which is the area of the largest square sub-matrix that contains only 1s.

## sample
2 2
1 1
1 1
4