#C5324. Maximal Square

    ID: 48961 Type: Default 1000ms 256MiB

Maximal Square

Maximal Square

Given a 2D grid of binary digits (0's and 1's), your task is to find the area of the largest square submatrix that contains only 1's.

The square must be composed entirely of 1's and its area is defined as the square of its side length. You are required to read the grid from standard input and output the area of the largest such square to standard output.

The problem can be formally stated as follows:

Given an integer matrix matrix of dimensions \( m \times n \), find the maximum area \( A = \ell^2 \) where \( \ell \) is the side length of a square comprised entirely of 1's.

Input Format: The first line contains two integers \( m \) and \( n \). The next \( m \) lines each contain \( n \) space-separated integers (each either 0 or 1).

Output Format: Output a single integer representing the area of the largest square found.

inputFormat

The first line of input contains two space-separated integers \( m \) and \( n \), the number of rows and columns respectively.

This is followed by \( m \) lines, each containing \( n \) space-separated integers (either 0 or 1) representing the 2D grid.

outputFormat

Output a single integer to stdout: the area of the largest square that can be formed using only 1's from the grid.

## sample
4 5
1 0 1 0 0
1 0 1 1 1
1 1 1 1 1
1 0 0 1 0
4