#K13031. Largest Connected Component in a Binary Grid
Largest Connected Component in a Binary Grid
Largest Connected Component in a Binary Grid
You are given a grid of size \(M \times N\) consisting of 0s and 1s. Your task is to find the size of the largest connected component containing 1s. Two cells are considered connected if they are adjacent horizontally or vertically. If there are no 1s in the grid, the answer is 0.
Input Description: The first line contains two integers, \(M\) and \(N\), representing the number of rows and columns. Each of the next \(M\) lines contains \(N\) integers (either 0 or 1), separated by spaces, representing the grid.
Output Description: Output a single integer which is the size of the largest connected component of 1s in the grid.
inputFormat
Standard Input (stdin): The first line contains two integers \(M\) and \(N\) separated by space. The following \(M\) lines each contain \(N\) space-separated integers (0 or 1) representing the grid.
outputFormat
Standard Output (stdout): A single integer denoting the size of the largest connected component of 1s.
## sample4 4
1 1 0 0
0 1 1 0
0 0 0 1
1 0 1 1
4