#K7361. Count the Number of Water Bodies
Count the Number of Water Bodies
Count the Number of Water Bodies
This problem requires you to count the number of distinct water bodies in a grid representing a lake. A water body is defined as a group of adjacent cells containing water ('W') that are connected horizontally or vertically. Land cells are represented by 'L'.
You will be given the dimensions of the grid, followed by the grid itself. If the grid is empty, the output should be 0. The connectivity condition can be mathematically represented as follows:
$$\text{If cell }(i,j) \text{ is } 'W', \text{ then its neighbors } (i+1,j), (i-1,j), (i,j+1), (i,j-1) \text{ are part of the same water body.}$$
Your task is to compute and output the number of distinct water bodies.
inputFormat
The first line of input contains two space-separated integers, r and c, which denote the number of rows and columns in the grid, respectively.
Each of the next r lines contains c characters separated by spaces. Each character is either 'W' (water) or 'L' (land).
outputFormat
Output a single integer which is the number of distinct water bodies in the grid.
## sample4 5
L W L L W
L W L W W
L L L W L
W W L L L
3