#C4888. Largest Square Subgrid with Equal Row Sums

    ID: 48475 Type: Default 1000ms 256MiB

Largest Square Subgrid with Equal Row Sums

Largest Square Subgrid with Equal Row Sums

You are given an m × n grid of integers. Your task is to find the side length of the largest square subgrid such that every row in the subgrid has the same sum.

Formally, consider a square subgrid with top‐left corner at \( (i, j) \) and side length \( s \). Let the elements of the subgrid be \( a_{k,\,l} \) for \( i \le k \le i+s-1 \) and \( j \le l \le j+s-1 \). We require that for every \( 0 \le t < s \), \[ \sum_{l=j}^{j+s-1} a_{i+t,\,l} = C \] for some constant \( C \). Output the maximum possible \( s \) that satisfies this property. Note that a single cell counts as a valid subgrid with \( s = 1 \).

Input Constraints: \( 1 \leq m, n \leq 10^3 \) (or as specified by the problem limits), and the grid elements are integers.

Examples:

Input:
4 5
1 2 3 4 5
5 1 2 1 3
1 0 4 -1 7
3 2 1 5 1

Output: 2

</p>

In the above example, the square subgrid of size 2 has rows with equal sums.

inputFormat

The first line of input contains two integers \( m \) and \( n \), representing the number of rows and columns of the grid. The following \( m \) lines each contain \( n \) space-separated integers, representing the grid.

Example:

4 5
1 2 3 4 5
5 1 2 1 3
1 0 4 -1 7
3 2 1 5 1

outputFormat

Output a single integer, the side length of the largest square subgrid where each row's sum is equal.

## sample
4 5
1 2 3 4 5
5 1 2 1 3
1 0 4 -1 7
3 2 1 5 1
2