All problems

Problem 179

Max Area of Island

Medium · 20 pts · Matrix, Depth-First Search, Breadth-First Search

Problem

You are given a grid where '1' is land and '0' is water. An island is a group of land cells connected horizontally or vertically, and its area is the number of cells in it. Print the area of the largest island, or 0 if the grid has no land at all. Scan the grid, and for each unvisited land cell flood fill its island with BFS or DFS, counting the cells as you mark them. Keep the largest count.

Input

First line: integers R and C. Next R lines: C characters, each '0' or '1'.

Output

Print the maximum island area.

Constraints

1 <= R, C <= 300

Example input

4 5
11000
11000
00100
00011

Example output

4

Explanation

The grid has three islands: the 2 x 2 block in the top-left corner (4 cells), the single cell in the middle (1 cell), and the pair in the bottom-right (2 cells). The largest area is 4.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Word Ladder Next: Tree Diameter