All tasks
Hardgraphsdfsarrays

Number of Islands

Time limit 2 s · Memory 64 MBAcceptance: —Solved by: 0

Statement

The first line contains n and m. Each of the next n lines contains m numbers: 1 is land, 0 is water. Print the number of islands. An island is a group of land cells connected by sides (left, right, up or down); diagonal adjacency does not count.

Examples

Example 1
Input
3 3
1 1 0
0 1 0
0 0 1
Output
2
Example 2
Input
1 1
0
Output
0

Constraints

  • 1 ≤ n, m ≤ 300
Solve this task to unlock the editorial.

AI tutor

Stuck? Get a nudge — the tutor explains the idea, it never hands you the solution.