#K37177. Count Islands

    ID: 25919 Type: Default 1000ms 256MiB

Count Islands

Count Islands

You are given a grid map of size \(n \times m\) where each cell of the grid is either L (land) or W (water). An island is defined as a maximal group of connected land cells. Two cells are considered connected if they are adjacent horizontally or vertically (but not diagonally).

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

Input Format:

  • The first line contains two integers n and m representing the number of rows and columns of the grid.
  • The following n lines each contain m characters (each being either L or W) separated by spaces.

Output Format:

  • Output a single integer that represents the number of islands in the grid.

Note: Use standard input (stdin) for reading input and standard output (stdout) for printing the result.

inputFormat

The first line contains two space-separated integers n and m.

Each of the next n lines contains m characters separated by a single space. Each character is either L (land) or W (water).

outputFormat

Output a single integer representing the number of islands.

## sample
1 1
L
1