#K68612. Count Islands

    ID: 32904 Type: Default 1000ms 256MiB

Count Islands

Count Islands

Given a grid of size N×MN \times M containing binary values (0 and 1), where 1 represents land and 0 represents water, count the number of islands. An island is formed by connecting adjacent land cells horizontally or vertically.

Input: The first line contains integers NN and MM. The next NN lines each contain MM integers representing the grid.

Output: Print a single integer representing the number of islands present in the grid.

inputFormat

Input is read from standard input (stdin). The first line contains two integers, NN and MM, representing the number of rows and columns, respectively. Then follow NN lines, each containing MM space-separated integers (0 or 1) representing the grid.

outputFormat

Output a single integer representing the number of islands present in the grid. The result is printed to standard output (stdout).## sample

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