Number of Islands

MediumGraphs
Next
GraphMatrixDFSUnion Find

A grid contains 1s (land) and 0s (water). An island is a group of land cells connected horizontally or vertically. Count the islands.

Input

Line 1: r and c. Next r lines: a string of c characters, each 0 or 1 (no spaces).

Output

Print the number of islands.

Constraints

1 <= r, c <= 50

Example 1
Input
4 5
11000
11000
00100
00011
Output
3

Three separate groups of land.

Hints

Number of Islands - Medium Graphs Problem | Skill Shiksha