197. Count the Islands
Core1000 ms256 MBSolved by 0%
A grid holds 1 for land and 0 for water. An island is a group of land cells
connected up, down, left or right — diagonals do not connect.
Print how many separate islands the grid contains.
Constraints - `1 ≤ r, c ≤ 1000` - Each value is `0` or `1`.
Input
The first line contains two integers r and c.
Each of the next r lines contains c space-separated integers, each 0 or 1.
Output
Print one integer — the number of islands. Print 0 if there is no land at all.
Input4 4
1 1 0 0
1 1 0 0
0 0 1 0
0 0 0 1
Output3
Notehas one large island and two single cells that touch only diagonally
Input2 2
1 0
0 1
Output2
Notehas two cells touching only at a corner, which does NOT connect them
Hints
Four rungs, in order. The last two open once you have submitted an attempt — a wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first — this one carries on from it.
Hint 3PseudocodeOpen hint 2 first — this one carries on from it.
Hint 4Full solutionOpen hint 3 first — this one carries on from it.