#C1384. Count the Number of Islands
Count the Number of Islands
Count the Number of Islands
You are given a grid of size \(n \times m\) where each cell is either land ('L') or water ('W'). An island is defined as a group of adjacent land cells connected horizontally or vertically (diagonal connections are not considered). Your task is to count the total number of islands in the grid.
The problem can be mathematically modeled as follows: \[ \text{islands} = \{ S \subseteq \{(i,j)\} : S \text{ is maximally connected and } \forall (i,j) \in S, \ grid[i][j] = 'L' \} \]
Be sure to process the input from stdin
and output your answer to stdout
.
inputFormat
The first line contains two integers n
and m
representing the number of rows and columns in the grid respectively. Each of the next n
lines contains a string of length m
made up of the characters 'L' and 'W', representing land and water.
Input Format:
n m row1 row2 ... rown
outputFormat
Output a single integer which is the number of islands found in the grid.
## sample3 3
LLL
LLL
LLL
1