#C14880. Island Counter

    ID: 44578 Type: Default 1000ms 256MiB

Island Counter

Island Counter

You are given a 2D grid of size (m \times n) representing a map. Each cell in the grid is either '1' representing land or '0' representing water. An island is formed by connecting adjacent lands horizontally or vertically. The grid is surrounded by water on all edges. Your task is to count the number of islands present in the grid.

Example 1:

Input:
4 5
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0

Output:
1

Example 2:

Input:
4 5
1 1 0 0 0
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1

Output:
3

Note that if the grid is empty (i.e. (m=0)), the output should be 0.

inputFormat

The input is read from standard input (stdin). The first line contains two integers (m) and (n) representing the number of rows and columns of the grid respectively. If (m > 0), the next (m) lines each contain (n) space-separated characters (each either 0 or 1) denoting the grid cells.

outputFormat

Output a single integer to standard output (stdout) representing the number of islands in the given grid.## sample

4 5
1 1 1 1 0
1 1 0 1 0
1 1 0 0 0
0 0 0 0 0
1