#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
int n,m,k=0,o=1,o2=1;
int x1,x2,y1,y2;
char v[100][100];
int g[100][100],b[100][100],c[100][100];
int x[]={-1,1,-1,1,0,-1,0,1};
int y[]={-1,-1,1,1,-1,0,1,0};
void test2(int o,int i, int j);
void test3(int o2,int i, int j);
void citire()
{
int i;
in>>n>>m;
for(i=0;i<=n;i++)
in.getline(v[i],100);
}
void cautare()
{
int i,j;
for(i=1;i<=n;i++)
for(j=0;j<m;j++)
{
if(v[i][j]!='X' && v[i][j]!='R' && v[i][j]!='J')
{
b[i][j]=-1;
c[i][j]=-1;
}
if(v[i][j]=='R')
{
x1=i;y1=j;
b[i][j]=0;
c[i][j]=0;
}
if(v[i][j]=='J')
{
x2=i;
y2=j;
b[i][j]=0;
c[i][j]=0;
}
}
}
void test()
{
test2(2,x1,y1);
test3(2,x2,y2);
}
void test2(int o,int i, int j)
{
int l;
for(l=0;l<8;l++)
{
if(b[i+x[l]][j+y[l]]<0)
{
b[i+x[l]][j+y[l]]=o;
test2(o+1,i+x[l],j+y[l]);
}
}
}
void test3(int o2,int i,int j)
{
int l;
for(l=0;l<8;l++)
{
if(c[i+x[l]][j+y[l]]<0)
{
c[i+x[l]][j+y[l]]=o2;
test3(o2+1,i+x[l],j+y[l]);
}
}
}
int main()
{
int i,j,max=0,col,lin;
citire();
cautare();
test();
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
cout<<b[i][j]<<" ";
cout<<endl;
}
cout<<endl;
for(i=1;i<=n;i++)
{
for(j=0;j<=n;j++)
cout<<c[i][j]<<" ";
cout<<endl;
}
for(i=1;i<=n;i++)
for(j=0;j<=n;j++)
if(c[i][j]==b[i][j] && c[i][j]!=0 && c[i][j]!=-1 && b[i][j]!=0 && b[i][j]!=-1)
{
max=c[i][j];
lin=i;
col=j;
}
cout<<lin<<col+2<<max-1;
return 0;
}