#K9741. Taco - Largest Palindromic Subgrid

    ID: 39097 Type: Default 1000ms 256MiB

Taco - Largest Palindromic Subgrid

Taco - Largest Palindromic Subgrid

You are given a grid of characters with n rows and m columns. Your task is to find a subgrid (a contiguous block of cells) which is palindromic in both its rows and columns. A subgrid is considered palindromic if every row in the subgrid reads the same forwards and backwards, and every column reads the same top-to-bottom and bottom-to-top.

The size of a subgrid is defined as the number of rows in that subgrid. Your goal is to determine the maximum possible size of such a subgrid. Note that any single cell is always considered palindromic.

Example 1:
Input grid:

ABA
BAB
ABA
The entire grid is palindromic, so the answer is 3.

Example 2:
Input grid:

AAAA
ABBA
ABBA
AAAA
The answer is 4.

Example 3:
Input grid:

ABC
DEF
No subgrid larger than a single cell is palindromic, so the answer is 1.

inputFormat

The first line contains two space-separated integers n and m. Following this, there are n lines, each containing a string of exactly m characters representing the grid.

outputFormat

Output a single integer: the side length (i.e. the number of rows) of the largest palindromic subgrid found.## sample

3 3
ABA
BAB
ABA
3