#K49762. Maximum Area of Island
Maximum Area of Island
Maximum Area of Island
You are given an m x n grid of integers where each element is either 0 or 1. A cell with value 1 represents land and a cell with value 0 represents water. An island is a group of adjacent lands connected horizontally or vertically. Your task is to compute the maximum area (i.e., the number of cells) of an island in the grid. If there is no island, the maximum area is 0.
The area of an island is the total number of connected 1's. Use Depth-First Search (DFS) or Breadth-First Search (BFS) to traverse the grid. Formally, if we denote a cell by its indices (i, j), two cells (i, j) and (k, l) are connected if \( |i-k| + |j-l| = 1 \).
Input/Output Format: In this problem, the input is read from stdin and the output is printed to stdout.
inputFormat
The first line of input contains two space-separated integers m
and n
, representing the number of rows and columns in the grid, respectively. The next m
lines each contain n
space-separated integers (either 0 or 1), describing the grid.
Example:
5 5 0 0 1 1 0 0 1 1 0 0 0 0 0 0 0 1 1 0 0 1 1 1 1 0 1
outputFormat
Print a single integer representing the area of the largest island in the grid.
Example:
5## sample
5 5
0 0 1 1 0
0 1 1 0 0
0 0 0 0 0
1 1 0 0 1
1 1 1 0 1
5