#P2177. Find the Largest Memory Killer Submatrix

    ID: 15458 Type: Default 1000ms 256MiB

Find the Largest Memory Killer Submatrix

Find the Largest Memory Killer Submatrix

Our great KK has recently been obsessed with movies. However, he never goes to the cinema because he is always broke and has no one to accompany him. To watch movies continuously, he uses the magical "Baidu Video" software which allows him to download movies while watching them. One day, KK noticed that his computer started slowing down. When his father came to check, KK had to quickly shut everything down. Upon inspecting the registry, he discovered that the culprit was hidden in a square matrix in memory. He calls such matrices "memory killers".

A square matrix (of side length greater than \(1\)) is defined as a memory killer if it remains unchanged after a \(180^\circ\) rotation. Formally, a square matrix \(A\) of side length \(L\) (with \(L \ge 2\)) is a memory killer if for all \(0 \le i, j < L\):

[ A[i][j] = A[L-1-i][L-1-j] ]

For example, the following matrices are memory killers:

[ \begin{bmatrix} 0 & 0 \ 0 & 0 \end{bmatrix}, \quad \begin{bmatrix} 1 & 0 \ 0 & 1 \end{bmatrix}, \quad \begin{bmatrix} 1 & 0 & 1 \ 1 & 1 & 1 \ 1 & 0 & 1 \end{bmatrix} ]

Given a rectangular memory region represented as a matrix, find the largest square (with side length at least 2) that is a memory killer. If there is no memory killer in the matrix, output -1.

inputFormat

The first line of input contains two integers \(n\) and \(m\) representing the number of rows and columns in the matrix respectively. The following \(n\) lines each contain \(m\) integers separated by spaces, representing the elements of the matrix.

outputFormat

Output a single integer denoting the side length of the largest memory killer submatrix (with side length at least 2). If no such submatrix exists, output -1.

sample

3 3
1 0 1
1 1 1
1 0 1
3