#K62242. Largest Contiguous Area of Land
Largest Contiguous Area of Land
Largest Contiguous Area of Land
You are given a grid of dimensions M x N where each cell is either land (1) or water (0). A contiguous area of land is a group of adjacent cells containing 1's that are connected horizontally or vertically (diagonals are not considered connected).
Your task is to determine the area (i.e., the number of cells) of the largest contiguous piece of land in the given grid. This problem can be efficiently solved using depth-first search (DFS) or breadth-first search (BFS).
Example:
- Input:
4 5
0 1 0 0 1
1 1 0 0 0
0 0 1 1 1
0 0 0 0 1 - Output:
4
inputFormat
The first line of input contains two integers, M and N, separated by a space. The next M lines each contain N integers (each either 0 or 1) separated by spaces, representing the grid.
outputFormat
Output a single integer representing the size of the largest contiguous land area.## sample
4 5
0 1 0 0 1
1 1 0 0 0
0 0 1 1 1
0 0 0 0 1
4