Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/matritsa-orollar-soni

Number of islands

Medium 1000 ms 128 MB 59%
Share

A map of size $n \times m$ is given: $1$ is land, $0$ is water. Land cells touching by their top, bottom, left or right side form one island (touching diagonally does not count). Find the number of islands on the map.

Connected land islands surrounded by water

Input

The first line contains two positive integers $n$ and $m$. Each of the next $n$ lines contains $m$ numbers ($0$ or $1$).

Output

One integer — the number of islands.

Note

$1 \le n, m \le 200$
$a_{i,j} \in \{0, 1\}$

Examples

Input #1
4 5
1 1 0 0 1
1 0 0 1 1
0 0 1 0 0
1 0 1 0 1
Output #1
5
Input #2
2 2
1 0
0 1
Output #2
2

Sign in to submit a solution

Signing up is free and takes a minute.