#K96362. Number of Land Regions

    ID: 39070 Type: Default 1000ms 256MiB

Number of Land Regions

Number of Land Regions

Given an \(n \times m\) grid of characters, where each cell is either land ('L') or water ('W'), your task is to determine the number of distinct land regions. A land region is defined as a maximal set of adjacent cells marked 'L', where cells are considered adjacent if they share a common side (up, down, left, right).

In other words, you need to count the connected components of 'L' in the grid.

inputFormat

The input is read from stdin and is formatted as follows:

  • The first line contains two integers \(n\) and \(m\) representing the number of rows and columns of the grid.
  • The next \(n\) lines each contain a string of \(m\) characters, where each character is either 'L' (land) or 'W' (water).

outputFormat

Output a single integer to stdout representing the number of distinct land regions.

## sample
5 5
LWWLL
LLWLL
WLLLL
WWWWW
LLLWL
3