#K11346. Largest Connected Component in a Grid
Largest Connected Component in a Grid
Largest Connected Component in a Grid
You are given a grid with n rows and m columns, where each cell contains either 0 or 1. A cell with value 1 represents land and a cell with value 0 represents water. Two cells are considered connected if they are adjacent vertically or horizontally.
Your task is to determine the size of the largest connected component of land cells. The input will be provided via stdin and the output should be printed to stdout.
Formally, if we define a component as a set of cells connected through the four cardinal directions, you need to compute the maximum number of cells in any such component.
Note: All indices and cells are considered in their natural order as they appear in the grid.
inputFormat
The first line contains two integers n and m separated by a space, representing the number of rows and columns respectively.
The next n lines each contain m integers (either 0 or 1) separated by spaces, representing the grid.
outputFormat
Output a single integer representing the size of the largest connected component of land cells (cells with value 1) in the grid.
## sample4 5
1 0 0 1 0
1 1 0 1 0
0 0 0 0 0
0 1 1 1 1
4