Marble Escape

A board holds a red marble, a blue marble and one hole; tilting slides both marbles at once, and you must decide whether the red marble can drop into the hole within 10 tilts without the blue marble falling in.

Medium7BFSSimulationImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

A rectangular board holds one red marble and one blue marble. The puzzle is to get the red marble out through the hole.

The board has NN rows and MM columns, split into cells of size 1×11 \times 1. The outermost row and column are blocked all the way around, and the board has exactly one hole. Each marble fills a 1×11 \times 1 cell completely, and there is one marble of each color. The goal is to drop the red marble through the hole. The blue marble must not go into the hole.

You cannot touch the marbles with your hands. You tilt the board and let gravity roll them. Four moves are available: tilt left, tilt right, tilt up, and tilt down.

One tilt moves both marbles at the same time, and the tilt continues until neither marble moves any further. Dropping the red marble into the hole is a success. Dropping the blue marble into the hole is a failure. If both marbles fall into the hole on the same tilt, that is also a failure.

The two marbles can never share a cell, and each one takes up a full cell. If a tilt would leave both marbles in the same cell, the marble that traveled farther on that tilt backs up one cell and stops right behind the other one.

Given the state of the board, write a program that decides whether the red marble can be removed in 10 tilts or fewer.

Input

The first line contains two integers NN and MM (3N,M103 \le N, M \le 10), the number of rows and columns of the board. Each of the next NN lines contains a string of length MM describing one row. The string consists of ., #, O, R, and B. Here . is an empty cell, # is an obstacle or wall that a marble cannot pass, O is the hole, R is the position of the red marble, and B is the position of the blue marble.

Every cell on the edge of the given board is #. There is exactly one hole, exactly one red marble, and exactly one blue marble.

Output

Print 1 if the red marble can be removed in 10 tilts or fewer without the blue marble falling into the hole, and 0 otherwise.