#C6239. Count Islands

    ID: 49977 Type: Default 1000ms 256MiB

Count Islands

Count Islands

Given a grid of size \(N \times M\) representing a field where each cell is either land ('L') or water ('W'), determine the number of distinct islands. Two cells are considered part of the same island if they are horizontally or vertically adjacent.

An island is a connected group of land cells. Your task is to count how many islands are present in the grid.

Note: Use depth-first search (DFS) or breadth-first search (BFS) to traverse and mark visited cells.

inputFormat

The first line contains two integers \(N\) and \(M\) (the number of rows and columns, respectively). Each of the next \(N\) lines contains \(M\) characters separated by spaces. Each character is either 'L' (land) or 'W' (water).

For example:

4 5
L W W L L
L L W W L
W W L W W
L W L L L

outputFormat

Output a single integer representing the number of islands found in the grid.

## sample
4 5
L W W L L
L L W W L
W W L W W
L W L L L
4