#K43972. Longest Contiguous Identical Subarray

    ID: 27428 Type: Default 1000ms 256MiB

Longest Contiguous Identical Subarray

Longest Contiguous Identical Subarray

Given a grid of size \(n \times m\) filled with digits ranging from 1 to 9, your task is to find the length of the longest contiguous subarray with identical elements which can appear in any row or column.

A contiguous subarray means a consecutive sequence of elements. Note that the contiguous subarray can only be horizontal (in a row) or vertical (in a column), not diagonal.

Input Constraints: \(1 \leq n, m \leq 10^3\) and grid values are between 1 and 9.

Example: For the grid below:

\[ \begin{bmatrix} 1 & 2 & 2 \\ 2 & 2 & 2 \\ 3 & 3 & 3 \end{bmatrix} \]

the longest contiguous subarray with identical elements is of length 3.

inputFormat

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

The following \(n\) lines each contain \(m\) integers separated by spaces, representing the grid.

outputFormat

Output a single integer representing the length of the longest contiguous subarray with identical elements found in any row or column.

## sample
3 3
1 2 2
2 2 2
3 3 3
3