Block Game

A ball launched at 45 degrees bounces off three walls of a grid; count how many distinct polyomino blocks its 45-degree path passes through the interior of.

Medium7ImplementationGeometrySimulationBrute forceNo attempts yetTime limit1sMemory limit128 MB

Problem

Games where you fire a ball at blocks on a grid and break them have a long history, from Breakout, made by Atari in 1976, through Arkanoid and many later titles. Hyeonjong and Taehyeon are competing over the record in one of these games. Hyeonjong has already finished his turn, and Taehyeon's score decides who takes the penalty, which is setting up every desk on the final round stage alone.

The rules are simple. The board is the region 0yM0 \le y \le M, 0xN0 \le x \le N. Fire the ball from a point on the bottom segment y=0y = 0 at 45 degrees up and to the left. The ball travels in a straight line, reflects when it meets a wall, and the turn ends the moment it reaches y=0y = 0 again. The walls are the three segments x=0x = 0, x=Nx = N, and y=My = M, the segments that enclose the board except for y=0y = 0, which is both the launch line and the finish line. If the ball reaches the corner where two walls meet, it reflects off both walls and retraces the path it came from.

The blocks sit exactly on a grid with MM rows and NN columns. Removing one block scores 1 point. A single block can be made of several connected cells, where connected means two cells share an edge. Three cells can form an L shape and count as one block, and the shape can be more complicated than that. Two cells that share an edge can also belong to two different blocks.

Taehyeon has one last ball that decides the match. The last ball has a special power: it passes through a block as if the block were not there and destroys it on the way. You never have to think about what happens when the ball meets a block, only about the blocks the ball sweeps through while bouncing off the walls.

A block is destroyed when the path of the ball passes through the interior of one of its cells. Passing through several cells of the same block still scores 1 point. If the path only grazes a cell at a single corner point, the block in that cell is not destroyed.

Wonha, eating popcorn on the side, decided to think for a moment about where Taehyeon should fire. Given the state of the board and the launch position, find how many blocks Taehyeon destroys.

Input

The first line has two integers NN and MM (1N,M1001 \le N, M \le 100), the size of the board, and the launch position KK of the ball. KK is greater than 0, smaller than NN, and a multiple of 0.5.

The next 2M+12M+1 lines describe the state of the board with 2N+12N+1 characters per line. Each character is one of +, |, -, B, O, ..

The character in position jj (1j2N+11 \le j \le 2N+1) of line ii (1i2M+11 \le i \le 2M+1) of the board is as follows.

  • ii and jj both odd: the character is +. It only separates cells.
  • ii and jj both even: the character is B or O. It stands for the cell whose opposite corners are (j/21, Mi/2)(j/2 - 1,\ M - i/2) and (j/2, M+1i/2)(j/2,\ M + 1 - i/2), where B means the cell holds a block and O means it holds none.
  • Otherwise: the character is one of |, -, .. It gives the relation between the two cells on either side of it. If both cells hold blocks and the two are one block, the character is .. In every other case it is - when ii is odd and | when jj is odd.

Output

Print the score of the last turn on the first line.

Hint

How the first example goes.