Checkers

Count the black kings that can capture all white kings in one chain of diagonal jumps.

Medium5BacktrackingDFSNo attempts yetTime limit2sMemory limit256 MB

Problem

Checkers is played on a square board of n×nn \times n cells. A real game uses nn equal to 8, 10, or 12, but in this problem nn is between 2 and 26. The cells are colored red and black so that two cells sharing a side never have the same color, and every piece moves only on the black cells.

The two players are Black and White, and their pieces carry the same colors. Checkers has two kinds of pieces, checkers and kings, but this problem uses kings only.

A king jumps a piece of the other color standing diagonally next to it and lands on the cell directly beyond that piece, capturing it. The captured piece leaves the board at once. After a capture the same king may keep jumping while another piece of the other color can be jumped, and the whole chain of jumps counts as one move. A king may jump in any of the four diagonal directions.

A jump is legal only if the piece to be jumped is diagonally adjacent to the jumping king and the cell directly beyond that piece lies inside the board and is empty.

It is Black's turn. Given a position, count the black kings that can capture every white king in a single move. If the board holds no white king, no jump exists, so the answer is 0.

Input

The first line contains the board size nn (2n262 \le n \le 26). Each of the next nn lines describes one row of the board and contains exactly nn characters. Each character is one of ., _, B, W:

  • . is a red cell. No king can stand on a red cell.
  • _ is an empty black cell.
  • B is a black cell holding a black king.
  • W is a black cell holding a white king.

The given board is always well formed: black and red cells alternate along every row and every column, and no king stands on a red cell.

Output

Print one integer, the number of black kings that can capture every white king in a single move.