#include <iostream>
#include <algorithm>
#include <cstring>
#define MAX_WIDTH 10
#define MAX_DEEP 10
using namespace std;
char map[MAX_WIDTH][MAX_WIDTH];
int width;
int height;
int R[2];
int B[2];
int Hole[2];
int minDeep;
void getProperties();
void findMinWay(int tempR[], int tempB[], int deep = 0);
void print(int tempR[], int tempB[], int deep);
int main() {
getProperties();
minDeep = 100;
findMinWay(R, B);
if (minDeep == 100)
cout << -1;
else
cout << minDeep;
}
void getProperties() {
cin >> height >> width;
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
cin >> map[h][w];
if (map[h][w] == 'R') {
R[0] = h;
R[1] = w;
}
else if (map[h][w] == 'B') {
B[0] = h;
B[1] = w;
}
else if (map[h][w] == 'O') {
Hole[0] = h;
Hole[1] = w;
}
}
}
}
void findMinWay(int tempR[], int tempB[], int deep) {
if (tempB[0] == Hole[0] && tempB[1] == Hole[1])
return;
if (tempR[0] == Hole[0] && tempR[1] == Hole[1]) {
if (deep < minDeep) {
minDeep = deep;
}
return;
}
if (deep >= MAX_DEEP)
return;
int currentR[2], currentB[2];
memcpy(currentR, tempR, sizeof(tempR)*2);
memcpy(currentB, tempB, sizeof(tempB)*2);
//print(tempR, tempB, deep);
//์
while (map[currentR[0]-1][currentR[1]] != '#' && map[currentR[0]][currentR[1]] != 'O')
currentR[0]--;
while (map[currentB[0]-1][currentB[1]] != '#' && map[currentB[0]][currentB[1]] != 'O')
currentB[0]--;
if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
if (map[currentB[0]][currentB[1]] != 'O') {
if (tempR[0] < tempB[0])
currentB[0]++;
else
currentR[0]++;
}
findMinWay(currentR, currentB, deep + 1);
memcpy(currentR, tempR, sizeof(int) * 2);
memcpy(currentB, tempB, sizeof(int) * 2);
//์ค๋ฅธ์ชฝ
while (map[currentR[0]][currentR[1]+1] != '#' && map[currentR[0]][currentR[1]] != 'O')
currentR[1]++;
while (map[currentB[0]][currentB[1]+1] != '#' && map[currentB[0]][currentB[1]] != 'O')
currentB[1]++;
if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
if (map[currentB[0]][currentB[1]] != 'O') {
if (tempR[1] < tempB[1])
currentR[1]--;
else
currentB[1]--;
}
findMinWay(currentR, currentB, deep + 1);
memcpy(currentR, tempR, sizeof(int) * 2);
memcpy(currentB, tempB, sizeof(int) * 2);
//์๋
while (map[currentR[0]+1][currentR[1]] != '#' && map[currentR[0]][currentR[1]] != 'O')
currentR[0]++;
while (map[currentB[0]+1][currentB[1]] != '#' && map[currentB[0]][currentB[1]] != 'O')
currentB[0]++;
if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
if (map[currentB[0]][currentB[1]] != 'O') {
if (tempR[0] < tempB[0])
currentR[0]--;
else
currentB[0]--;
}
findMinWay(currentR, currentB, deep + 1);
memcpy(currentR, tempR, sizeof(int) * 2);
memcpy(currentB, tempB, sizeof(int) * 2);
//์ผ์ชฝ
while (map[currentR[0]][currentR[1]-1] != '#' && map[currentR[0]][currentR[1]] != 'O')
currentR[1]--;
while (map[currentB[0]][currentB[1]-1] != '#' && map[currentB[0]][currentB[1]] != 'O')
currentB[1]--;
if (currentR[0] == currentB[0] && currentR[1] == currentB[1])
if (map[currentB[0]][currentB[1]] != 'O') {
if (tempR[1] < tempB[1])
currentB[1]++;
else
currentR[1]++;
}
findMinWay(currentR, currentB, deep + 1);
memcpy(currentR, tempR, sizeof(int) * 2);
memcpy(currentB, tempB, sizeof(int) * 2);
}
void print(int tempR[], int tempB[], int deep) {
cout << "deep : " << deep << "/ R : " << tempR[0] << ' ' << tempR[1] << "/ B : " << tempB[0] << ' ' << tempB[1] << endl;
}