#K61112. Largest Connected Group

    ID: 31238 Type: Default 1000ms 256MiB

Largest Connected Group

Largest Connected Group

Given an \(n \times m\) grid filled with integers, determine the size of the largest connected group of cells that contain the same integer. Two cells are connected if they share an edge (up, down, left or right). For example, consider the grid:

1 2 2 3 4
2 2 2 3 4
3 3 2 0 0
4 0 0 0 0

The largest connected group has a size of 6. Solve the problem by reading input from standard input (stdin) and writing the answer to standard output (stdout).

inputFormat

The first line contains two integers \(n\) and \(m\) — the number of rows and columns in the grid. Each of the following \(n\) lines contains \(m\) space-separated integers representing the grid.

Example:

4 5
1 2 2 3 4
2 2 2 3 4
3 3 2 0 0
4 0 0 0 0

outputFormat

Output a single integer representing the size of the largest connected group where all cells have the same value.

Example Output:

6
## sample
4 5
1 2 2 3 4
2 2 2 3 4
3 3 2 0 0
4 0 0 0 0
6