#include<cstdio>
const int dr[] = { 0,0,1,-1 };
const int dc[] = { 1,-1,0,0 };
const int M = 11;
int ROW, COL;
struct pos {
int r, c;
};
int dfs(int tried_dir, int tried, pos red, pos blue, char mp[][M]) {
int ret = 0;
if (tried > 9) return ret; // 10 -> 9
char nimap[M][M];
pos nred, nblue;
for(int i = 0 ; i < 4 ; i++){
if (tried_dir == i) continue;
bool red_ok = false;
bool blue_ok = false;
for (int r = 0; r < ROW; r++) {
for (int c = 0; c < COL; c++) nimap[r][c] = mp[r][c];
}
nred = red;
nblue = blue;
bool blue_is_wall = false;
while (1) {
int hred_r = nred.r + dr[i];
int hred_c = nred.c + dc[i];
if (hred_r < 0 || ROW <= hred_r || hred_c < 0 || COL <= hred_c) break;
char next = nimap[hred_r][hred_c];
if (next == 'O') {
nimap[nred.r][nred.c] = '.';
red_ok = true;
break;
}
else if (next == '.') {
nimap[hred_r][hred_c] = 'R';
nimap[nred.r][nred.c] = '.';
nred.r = hred_r;
nred.c = hred_c;
}
else if (next == '#') break;
else if (next == 'B') {
if (blue_is_wall) break;
while (1) {
int hblue_r = nblue.r + dr[i];
int hblue_c = nblue.c + dc[i];
if (hblue_r < 0 || ROW <= hblue_r || hblue_c < 0 || COL <= hblue_c) break;
int blue_next = nimap[hblue_r][hblue_c];
if (blue_next == 'O') {
nimap[nblue.r][nblue.c] = '.';
blue_ok = true;
break;
}
else if (blue_next == '.') {
nimap[hblue_r][hblue_c] = 'B';
nimap[nblue.r][nblue.c] = '.';
nblue.r = hblue_r;
nblue.c = hblue_c;
}
else if (blue_next == '#') {
blue_is_wall = true;
break;
}
}
}
if (blue_ok) {
ret = 0;
break;
}
} // red logic
if (!blue_is_wall) {
while (1) {
int hblue_r = nblue.r + dr[i];
int hblue_c = nblue.c + dc[i];
if (hblue_r < 0 || ROW <= hblue_r || hblue_c < 0 || COL <= hblue_c) break;
int blue_next = nimap[hblue_r][hblue_c];
if (blue_next == 'O') {
blue_ok = true;
break;
}
else if (blue_next == '.') {
nimap[hblue_r][hblue_c] = 'B';
nimap[nblue.r][nblue.c] = '.';
nblue.r = hblue_r;
nblue.c = hblue_c;
}
else if (blue_next == '#' || blue_next == 'R') break;
} // blue_logic
}
if (!blue_ok && !red_ok) ret = dfs(i ,tried + 1, nred, nblue, nimap);
if (blue_ok) continue; // 멍청이
if (red_ok || ret == 1) return ret = 1;
}
return ret;
}
int main() {
char imap[M][M];
scanf("%d%d", &ROW, &COL);
pos red;
pos blue;
for (int r = 0; r < ROW; r++) {
scanf("%s", imap[r]);
for (int c = 0; c < COL; c++) {
if (imap[r][c] == 'R') {
red.r = r; red.c = c;
}
else if (imap[r][c] == 'B') {
blue.r = r; blue.c = c;
}
}
}
int ret = 0;
for (int i = 0; i < 4; i++) {
ret = dfs(-1, 0, red, blue, imap);
if (ret) {
puts("1");
break;
}
}
if (!ret) puts("0");
return 0;
}