#include<cstdio>
const int dr[] = { 0, -1, 0, 1 };
const int dc[] = { 1, 0, -1, 0 };
const int M = 11;
const int INF = 111;
int ROW, COL;
struct pos {
int r, c;
};
int mn2(int a, int b) {
return (a > b) ? b : a;
}
int dfs(int tried, pos red, pos blue, char mp[][M], int prev_dir) {
int ret = INF;
if (tried > 9) return ret;
for (int i = 0; i < 4; i++) {
if (prev_dir == i) continue;
if (prev_dir == ((i + 2) % 2)) continue;
char nimap[M][M];
for (int r = 0; r < ROW; r++)
for (int c = 0; c < COL; c++) nimap[r][c] = mp[r][c]; // copy map
int hr_r = red.r;
int hr_c = red.c;
int hb_r = blue.r;
int hb_c = blue.c;
bool red_ok = false;
bool blue_ok = false;
bool blue_is_wall = false;
while (1) {
int tr_r = hr_r + dr[i];
int tr_c = hr_c + dc[i];
char& red_here = nimap[hr_r][hr_c];
char& red_next = nimap[tr_r][tr_c];
if (red_next == '.') {
red_next = 'R';
red_here = '.';
hr_r = tr_r;
hr_c = tr_c;
}
else if (red_next == 'O') {
hr_r = tr_r;
hr_c = tr_c;
red_here = '.';
red_ok = true;
break;
}
else if (red_next == 'B') {
if (blue_is_wall) break;
while (1) {
int tb_r = hb_r + dr[i];
int tb_c = hb_c + dc[i];
char& blue_here = nimap[hb_r][hb_c];
char& blue_next = nimap[tb_r][tb_c];
if (blue_next == '.') {
blue_next = 'B';
blue_here = '.';
hb_r = tb_r;
hb_c = tb_c;
}
else if (blue_next == 'O') {
hb_r = tb_r;
hb_c = tb_c;
blue_here = '.';
blue_is_wall = true;
blue_ok = true;
break;
}
else if (blue_next == '#') {
blue_is_wall = true;
break;
}
} // blue
}
else if (red_next == '#') {
break;
}
} // red
if (!blue_is_wall) {
while (1) {
int tb_r = hb_r + dr[i];
int tb_c = hb_c + dc[i];
char& blue_here = nimap[hb_r][hb_c];
char& blue_next = nimap[tb_r][tb_c];
if (blue_next == '.') {
blue_next = 'B';
blue_here = '.';
hb_r = tb_r;
hb_c = tb_c;
}
else if (blue_next == 'O') {
hb_r = tb_r;
hb_c = tb_c;
blue_here = '.';
blue_ok = true;
break;
}
else if (blue_next == '#' || blue_next == 'R') {
break;
}
} // blue
}
pos nred;
nred.r = hr_r;
nred.c = hr_c;
pos nblue;
nblue.r = hb_r;
nblue.c = hb_c;
if (red_ok && !blue_ok) {
ret = mn2(tried + 1, ret);
}
else if (!red_ok && !blue_ok) {
ret = mn2(ret, dfs(tried + 1, nred, nblue, nimap, i));
}
}
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;
}
} // input
int ans = 111;
ans = dfs(0, red, blue, imap, -1);
if (ans == INF) puts("-1");
else printf("%d", ans);
return 0;
}