#K91022. Counting Islands

    ID: 37883 Type: Default 1000ms 256MiB

Counting Islands

Counting Islands

You are given a 2D grid of characters, where each character is either '1' (representing land) or '0' (representing water). An island is defined as a group of adjacent '1's connected horizontally or vertically (not diagonally). In mathematical terms, an island is a maximal set of cells such that for any two cells \( (i, j) \) and \( (k, l) \) in the island, there exists a sequence of cells connecting them where each consecutive pair is adjacent in one of the four cardinal directions.

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

Input Format: The first line contains two integers \( R \) and \( C \) representing the number of rows and columns respectively. The following \( R \) lines each contain a string of length \( C \) that represents a row in the grid.

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

inputFormat

The input is provided via standard input (stdin) and consists of:

  • The first line contains two space-separated integers \( R \) and \( C \) (\( 1 \leq R, C \leq 1000 \)).
  • The next \( R \) lines each contain a string of length \( C \) with characters '1' or '0'.

outputFormat

Output a single integer to standard output (stdout) indicating the number of islands found in the grid.

## sample
5 5
11000
11000
00100
00011
00011
3