#K72002. Island Counting

    ID: 33656 Type: Default 1000ms 256MiB

Island Counting

Island Counting

Given an n x n grid representing a map, each cell is either land ('L') or water ('W'). Your task is to count the number of islands. An island is defined as a group of adjacent land cells that are connected vertically or horizontally, and the grid is completely surrounded by water.

You may use depth-first search (DFS) or any other traversal method to mark visited cells and count each island. The answer must be computed for each test case provided via standard input and output the result via standard output.

The relationship can be expressed as: \(\text{islands} = \sum_{i,j} \mathbb{1}_{(\text{cell }(i,j) \text{ is the start of a new island})}\).

inputFormat

The input is given via stdin and has the following format:

T
n
row1
row2
... 
rown
... (repeated for each test case)

The first line contains an integer T representing the number of test cases. For each test case, the first line contains an integer n which represents the size of the grid (the grid has n rows and n columns). Each of the following n lines contains a string of exactly n characters, each is either 'L' (land) or 'W' (water).

For example, a test case might look like:

4
LWWL
LLWL
WWLW
LWLW

outputFormat

For each test case, output a single line via stdout containing one integer – the number of islands in the given grid.

## sample
4
4
LWWL
LLWL
WWLW
LWLW
3
LLL
LLW
LWW
3
WWW
WWW
WWW
3
LWL
WLW
LWL
4

1 0 5

</p>