LogiQuest

17. Number of Islands

MediumGraphDFSMatrix

A grid contains '1' (land) and '0' (water). Count the islands: groups of land cells connected horizontally or vertically.

Input format

Line 1: r c Next r lines: a string of c characters (0 or 1)

Output format

The number of islands.

Example 1

Input

4 5
11110
11010
11000
00000

Output

1

Example 2

Input

4 5
11000
11000
00100
00011

Output

3

Constraints

1 ≤ r, c ≤ 300

Read input from standard input and print the answer to standard output. Your code is judged against 5 test cases, most of them hidden.

Loading the MyLoginn IDE…