#include<stdio.h>
#pragma warning(disable:4996)
int que[11000][5];
int N, M;
int map[12][12];//체크는 빨간색과 파란색이 같은 곳에 있는지 확인해야함 그래서 빨강색이 체크chk[x][y]일때 파란색의 좌표가 [0][1];
int st = -1, ed = -1, chk = 0, ans = 0;
int chk_cnt[10000];
int blue[2], red[2];
int xm[4] = { 1,0,-1,0 }, ym[4] = { 0,1,0,-1 };
int main() {
char hold;
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
scanf(" %c", &hold);
if (hold == '.')map[i][j] = 0;
else if (hold == '#')map[i][j] = 1;
else if (hold == 'B') {
map[i][j] = 2;
blue[0] = i;
blue[1] = j;
}
else if (hold == 'R') {
map[i][j] = 3;
red[0] = i;
red[1] = j;
}
else if (hold == 'O')map[i][j] = 4;
}
}
que[++st][0] = red[0];
que[st][1] = red[1];
que[st][2] = blue[0];
que[st][3] = blue[1];
chk_cnt[(red[0] - 1) + (red[1] - 1) * 10 + (blue[0] - 1) * 100 + (blue[1] - 1) * 1000] = 1;
while (ans == 0 && st > ed) {
red[0] = que[++ed][0];
red[1] = que[ed][1];
blue[0] = que[ed][2];
blue[1] = que[ed][3];
if (que[ed][4] >= 11)break;
map[red[0]][red[1]] = 3;
map[blue[0]][blue[1]] = 2;
for (int i = 0; i < 4; i++) {
if (map[red[0] + ym[i]][red[1] + xm[i]] == 0) {//암것도 없으면 간다
chk = 1;
map[red[0]][red[1]] = 0;
while (map[red[0] + ym[i]][red[1] + xm[i]] == 0) {
red[0] += ym[i];
red[1] += xm[i];
}
map[red[0]][red[1]] = 3;
}
if (map[red[0] + ym[i]][red[1] + xm[i]] == 4) {//골인지점이면 간다
ans = 1;//빨간공이 일단 들어가긴 했음
map[red[0]][red[1]] = 0;
}
if (map[blue[0] + ym[i]][blue[1] + xm[i]] == 0) {
chk = 1;
map[blue[0]][blue[1]] = 0;
while (map[blue[0] + ym[i]][blue[1] + xm[i]] == 0) {
blue[0] += ym[i];
blue[1] += xm[i];
}
map[blue[0]][blue[1]] = 2;
}
if (map[blue[0] + ym[i]][blue[1] + xm[i]] == 4) {//파란공이 들어감
chk = 3;
ans = 0;
}
if (map[red[0] + ym[i]][red[1] + xm[i]] == 0) {//암것도 없으며 간다
map[red[0]][red[1]] = 0;
while (map[red[0] + ym[i]][red[1] + xm[i]] == 0) {//파란공에 막혔었는 문제 해결
red[0] += ym[i];
red[1] += xm[i];
}
map[red[0]][red[1]] = 3;
}
if (ans == 1) {
que[ed][4]++;
break;
}
if (chk == 1 && chk_cnt[(red[0] - 1) + (red[1] - 1) * 10 + (blue[0] - 1) * 100 + (blue[1] - 1) * 1000] == 0) {//움직였고, 그 포지션에 위치한적이 없으면
chk_cnt[(red[0] - 1) + (red[1] - 1) * 10 + (blue[0] - 1) * 100 + (blue[1] - 1) * 1000] = 1;
que[++st][0] = red[0];
que[st][1] = red[1];
que[st][2] = blue[0];
que[st][3] = blue[1];
que[st][4] = que[ed][4] + 1;
}
chk = 0;
map[red[0]][red[1]] = 0;
map[blue[0]][blue[1]] = 0;//현위치 초기화
map[que[ed][0]][que[ed][1]] = 3;
map[que[ed][2]][que[ed][3]] = 2;//다시 원래의 자리로 마킹
red[0] = que[ed][0];
red[1] = que[ed][1];
blue[0] = que[ed][2];
blue[1] = que[ed][3];//원래자리로 공 보내기
}
map[que[ed][0]][que[ed][1]] = 0;//맵에서 공을 지움
map[que[ed][2]][que[ed][3]] = 0;
}
if (ans == 1)printf("%d", que[ed][4]);
else printf("-1");
}