#K9641. Count Islands

    ID: 39075 Type: Default 1000ms 256MiB

Count Islands

Count Islands

You are given a grid of characters with '0' and '1', where '1' represents land and '0' represents water. The grid can be viewed as an $N \times M$ matrix. Two land cells are considered connected if they are adjacent horizontally or vertically. Your task is to count the number of islands (i.e. connected components of land) in the grid.

Note: Diagonal connections are not considered. For example, if two '1''s are adjacent only diagonally, they belong to separate islands.

The grid is provided via standard input, and your program should output the number of islands via standard output.

inputFormat

The input is given in the following format from standard input:

N M
row1
row2
...
rowN

Here, N and M are positive integers denoting the number of rows and columns in the grid, respectively. Each of the following N lines contains a string of length M consisting only of the characters '0' and '1'.

outputFormat

Output a single integer representing the number of islands in the grid to standard output.

## sample
4 5
11000
11010
00000
01110
3

</p>