#P10475. Repetitive Rectangular Pattern

    ID: 12487 Type: Default 1000ms 256MiB

Repetitive Rectangular Pattern

Repetitive Rectangular Pattern

Every morning when they are milked, Farmer John's cows form a rectangular grid of R rows and C columns, where \(1 \le R \le 10,000\) and \(1 \le C \le 75\). Each cow is labeled with an uppercase letter indicating its breed. Sometimes, the overall pattern appears to be made up of a smaller rectangular unit repeated throughout the grid.

The task is to find the rectangular unit of smallest area that can be tiled (repeated periodically) to form the entire grid. Formally, you are to find the smallest positive integers \(a\) and \(b\) such that for every cell at position \( (i, j) \) in the grid (0-indexed), the following holds:

[ grid[i][j] = grid[i \bmod a][j \bmod b] ]

Note that the dimensions \(a\) and \(b\) of the small rectangular unit do not necessarily need to evenly divide \(R\) and \(C\) respectively. The answer is the area of this minimal rectangular unit, i.e. \(a \times b\).

inputFormat

The first line contains two integers, \(R\) and \(C\). Each of the next \(R\) lines contains a string of \(C\) uppercase letters without spaces, representing the breed of each cow in that row.

outputFormat

Output a single integer representing the area (i.e. \(a \times b\)) of the smallest repeating rectangular pattern that tiles the entire grid.

sample

2 3
ABC
ABC
3

</p>