Go - MarisaOJ: Marisa Online Judge

Go

Time limit: 1000 ms
Memory limit: 256 MB
The problem shares some similarities with the game of Go, but it is not exactly Go. However, let's first go over the rules of Go a bit. In Go, there are two players, black and white, and stones are placed on the intersections of the lines on the board. **Liberties of a Stone**
The intersections adjacent to each stone are its liberties. Stones placed adjacent to each other belong to the same group and share the same liberties. Stones placed diagonally to each other, such as stones `A` and `B`, do not belong to the same group. **Capturing Stones**
When an opponent's stone is placed on the liberty of a player's stone, that liberty is removed. A group of stones with no liberties left is captured and removed from the board. For example, white stones placed at intersections `X` can capture the corresponding black stones marked with `O`. **Problem** Given a Go board with $n$ horizontal lines and $m$ vertical lines, and the board contains both black and white stones. For simplicity, assume only black stones can capture white stones. Count the number of white stones that are captured. ### Input - The first line contains two integers $n$ and $m$. - The next $n$ lines each contain a string of $m$ characters representing the board: + Character `W` represents a white stone. + Character `B` represents a black stone. + Character `.` represents an empty intersection. ### Output - Output an integer representing the number of white stones that are captured. ### Constraints - $1 \le n, m \le 500$. ### Example Input: ``` 5 5 WWBWB WBB.B B.WB. .BBWB ...B. ``` Output: ``` 4 ```