#C13223. Count Islands in a Grid

    ID: 42738 Type: Default 1000ms 256MiB

Count Islands in a Grid

Count Islands in a Grid

You are given a two-dimensional grid of size \( n \times m \) where each cell contains either a 0 (water) or a 1 (land). An island is a group of connected 1s, where cells are considered connected if they are adjacent horizontally or vertically (diagonals are not considered connected).

Your task is to count the number of islands present in the grid.

Note: The input is given through the standard input and the answer should be written to the standard output.

inputFormat

The first line contains two integers \( n \) and \( m \) representing the number of rows and columns, respectively.

The next \( n \) lines each contain \( m \) space-separated integers (either 0 or 1) representing the grid.

outputFormat

Output a single integer which is the number of islands in the grid.

## sample
1 1
1
1