#C6735. Counting Islands in a Grid

    ID: 50528 Type: Default 1000ms 256MiB

Counting Islands in a Grid

Counting Islands in a Grid

You are given a two-dimensional grid of characters where each cell is either L (land) or W (water). An island is defined as a group of adjacent lands, where two cells are considered adjacent if they share an edge. Note that cells connected only diagonally are not considered adjacent.

Your task is to determine the number of islands in the grid.

In mathematical terms, if we denote the grid as a matrix \(G\) with \(n\) rows and \(m\) columns, then two cells \(G_{i,j}\) and \(G_{k,l}\) are adjacent if and only if \(|i-k| + |j-l| = 1\). An island is a maximal set of cells such that each cell is land (i.e. 'L') and every pair of cells in the set is connected through adjacent land cells.

inputFormat

The input is given via standard input and consists of the following:

The first line contains an integer (n) representing the number of rows in the grid. Each of the next (n) lines contains a string of equal length representing a row of the grid. Each character is either 'L' (land) or 'W' (water).

outputFormat

Output a single integer to standard output: the number of islands found in the grid.## sample

3
WWWW
WWWW
WWWW
0