#K93347. Count Isolated Black Regions

    ID: 38399 Type: Default 1000ms 256MiB

Count Isolated Black Regions

Count Isolated Black Regions

Given a 2D grid of characters with dimensions \(m \times n\), where each cell is one of the following: 'B' (black), 'W' (white), or 'X' (boundary), your task is to determine the number of isolated black regions in the grid. A region is defined as a maximal set of connected 'B' cells (two cells are connected if they share an edge). In other words, you need to count the connected components consisting solely of 'B' characters.

The input consists of multiple datasets. For each dataset, output the number of isolated black regions found in the given grid.

inputFormat

The input is read from standard input (stdin) and has the following format:

T
m n
row1
row2
... (m rows)
... (repeat for T test cases)

Where:

  • T is the number of test cases.
  • For each test case, the first line contains two integers m and n, representing the number of rows and columns.
  • The next m lines each contain n characters separated by spaces. Each character is either 'B', 'W', or 'X'.

outputFormat

For each test case, output the number of isolated black regions. The output should be printed to standard output (stdout) as a single line with the counts for each test case separated by a space.

## sample
2
5 5
X W W W X
W B B W W
W B W W W
W W W X W
X W W W X
3 4
X W W X
W B W W
X W B X
1 2