#K12871. Largest Island Size
Largest Island Size
Largest Island Size
You are given a 2D binary matrix with 0 and 1 entries. An island is a group of adjacent 1's connected horizontally or vertically. Your task is to find the size (i.e. the number of 1's) of the largest island in the matrix.
Formally, let \(matrix[i][j]\) denote the element in the \(i\)-th row and \(j\)-th column of the grid. Two cells \(matrix[i][j]\) and \(matrix[k][l]\) are connected if and only if \(|i-k| + |j-l| = 1\). Find the maximum size among all islands in the grid. If there is no island, return 0
.
inputFormat
The input is read from standard input (stdin) and is formatted as follows:
- The first line contains two integers \(m\) and \(n\), representing the number of rows and columns of the matrix.
- The next \(m\) lines each contain \(n\) space-separated integers (each either 0 or 1) representing the rows of the matrix.
outputFormat
Output a single integer to standard output (stdout) indicating the size of the largest island in the matrix.
## sample4 5
1 1 0 0 0
1 1 0 1 0
0 0 0 1 1
0 1 0 0 1
4