#K82392. Largest Island Size

    ID: 35965 Type: Default 1000ms 256MiB

Largest Island Size

Largest Island Size

Given a 2D grid where each cell contains a non-negative integer, your task is to determine the size of the largest island in the grid. A cell with a non-zero positive integer represents a piece of land, and a cell with 0 represents water. An island is defined as a group of adjacent cells (neighbors in the four cardinal directions: up, down, left, right) that share the same integer value (which is non-zero). If no islands exist in the grid, the output should be 0.

Mathematically, if the grid is denoted by \(G\) of size \(n \times m\) and \(G_{ij}\) is the value at the cell in row \(i\) and column \(j\), then an island is a set of cells \(\{(i,j)\}\) such that for any two cells \((i,j)\) and \((k,l)\) in the island there exists a path (using up, down, left, or right moves) with the property that for all cells \((p,q)\) in the path, \(G_{pq}=G_{ij}>0\). The goal is to compute \(\max\{ |\text{island}| \}\), where \(|\text{island}|\) is the number of cells in an island.

inputFormat

The input is given from the standard input (stdin). The first line contains two integers (n) and (m) representing the number of rows and columns in the grid. This is followed by (n) lines, each containing (m) space-separated integers, which represent the entries of the grid.

outputFormat

Output a single integer to the standard output (stdout) which is the size of the largest island.## sample

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