#C8760. Counting Distinct Islands

    ID: 52778 Type: Default 1000ms 256MiB

Counting Distinct Islands

Counting Distinct Islands

You are given a 2D grid of characters, where each cell is either '1' (land) or '0' (water). An island is a group of adjacent lands connected vertically or horizontally. Your task is to count the number of islands in the grid.

Note:

  • Two lands are considered connected if they are adjacent vertically or horizontally (not diagonally).
  • If a cell with '1' is not connected (in the above manner) to any other '1', it is considered an island by itself.

Input/Output: The input is provided from standard input (stdin) and the output must be written to standard output (stdout).

Example:

Input:
4 5
11000
11000
00100
00011

Output: 3

</p>

inputFormat

The first line contains two space-separated integers N and M, representing the number of rows and columns of the grid respectively.

This is followed by N lines, each containing a string of length M, consisting solely of the characters '0' and '1', representing the grid.

outputFormat

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

The answer must be printed to standard output.

## sample
4 5
11000
11000
00100
00011
3