#include <fstream>
#include <queue>
using namespace std;
ifstream fin("rj.in");
ofstream fout("rj.out");
char character;
int i, j, n, m, a[101][101], x_start, y_start, x2_start, y2_start, b[101][101];
int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[] = { 0, 1, 1, 1, 0, -1, -1, -1};
queue <pair<int,int>> q;
vector<pair<int,int>> drumr;
vector<pair<int,int>> drumj;
bool in_matrix(int x, int y)
{
if(x >= 1 && y >= 1 && x <= n && y <= m)
return true;
return false;
}
void lee(int a[101][101], int x_start, int y_start, int x_finish, int y_finish)
{
q.push({x_start, y_start});
a[x_start][y_start] = 1;
while(!q.empty())
{
int x_curent = q.front().first;
int y_curent = q.front().second;
for(i = 0; i <= 7; i++)
{
int x_vecin = x_curent + dx[i];
int y_vecin = y_curent + dy[i];
if(a[x_vecin][y_vecin] == 0 && in_matrix(x_vecin, y_vecin) == true)
{
q.push({x_vecin, y_vecin});
a[x_vecin][y_vecin] = a[x_curent][y_curent] + 1;
}
else if(x_start == x_finish && y_start == y_finish)
{
a[x_vecin][y_vecin] = a[x_curent][y_curent] + 1;
while(!q.empty())
q.pop();
return;
}
}
q.pop();
}
}
int main()
{
fin >> n >> m;
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
{
fin.get(character);
if(character == '\n')
fin.get(character);
if(character == 'X')
a[i][j] = -1;
else if(character == ' ')
a[i][j] = 0;
else if(character == 'J'){
a[i][j] = -3;
x2_start = i;
y2_start = j;
}
else if(character == 'R')
{
a[i][j] = -2;
x_start = i;
y_start = j;
}
}
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
b[i][j] = a[i][j];
lee(a, x_start, y_start, x2_start, y2_start);
lee(b, x2_start, y2_start, x_start, y_start);
for(i = 1; i <= n; i++)
for(j = 1; j <= m; j++)
if(a[i][j] == b[i][j] && a[i][j] != 0 && a[i][j] != -1)
{
fout << a[i][j] << ' ' << i << ' ' << j;
return 0;
}
}
/// -1, nu pot merge
/// 0, pot merge
/// -2, start, il consider pe Romeo start
/// -3, finish
/*
5 5
R XX
X X X
X XXX
X X X
X J X
*/
///se vor intalni peste nr de pasi a lui romeo / 2 + 1 (5 / 2 + 1)