#C6318. Count the Number of Districts
Count the Number of Districts
Count the Number of Districts
You are given a grid representing a city map with land ('L') and water ('W'). Two adjacent cells (horizontally or vertically) that are land belong to the same district. Your task is to determine the number of separate districts in the city.
The grid is described by two integers \(n\) and \(m\), which denote the number of rows and columns respectively. This is followed by \(n\) strings of length \(m\), each consisting solely of the characters 'L' (for land) and 'W' (for water).
Note: Two cells sharing a common side are considered adjacent. Diagonally adjacent cells are not connected.
inputFormat
The input is given from stdin in the following format:
n m row1 row2 ... rown
Here, the first line contains two space-separated integers \(n\) and \(m\) \( (1 \leq n, m \leq 1000) \), representing the number of rows and columns. Each of the following \(n\) lines contains a string of length \(m\) that describes a row of the grid. Each character in the string is either 'L' (land) or 'W' (water).
outputFormat
Output a single integer to stdout: the number of districts (i.e., connected groups of land) in the grid.
## sample4 5
LWLWW
LWLWL
WWWLL
LWLLW
4