#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define dim 102
#define dim2 10100
short int n,m,i,j,x1,x2,y1,y2,mat[dim][dim],t,x,y,a[dim2],b[dim2],c[dim2],d[dim2],mat2[dim][dim];
short int dir1[9]={0,-1,-1,0,1,1,1,0,-1};
short int dir2[9]={0,0,1,1,1,0,-1,-1,-1};
char cc;
int main()
{
FILE *f=fopen("rj.in","r"), *g=fopen("rj.out","w");
fscanf(f,"%hd%hd\n",&n,&m);
for(i=1;i<=n;i++)
{for(j=1;j<=m;j++)
{fscanf(f,"%c",&cc);
if(cc=='R') {x1=i;y1=j;}
else if(cc=='J') {x2=i;y2=j;}
else if(cc=='X') {mat[i][j]=-1;mat2[i][j]=-1;}
}
fscanf(f,"%c ",&cc);
}
t=2;
x=1; y=1;
mat[x1][y1]=-1;
mat[x2][y2]=-1;
mat2[x1][y1]=-1;
mat2[x2][y2]=-1;
a[1]=x1; c[1]=x2;
b[1]=y1; d[1]=y2;
int ok=1,s;
while(ok)
{s=x;
//romeo
while(s<=y)
{ for(i=1;i<=8;i++)
if((x1+ dir1[i]==c[1])&&( y1 + dir2[i]==d[1]))
{
ok=0; s=y+1; break;
}
else
{
if(mat[ x1+ dir1[i] ][ y1+ dir2[i]]==0)
if(( x1+ dir1[i] <=n)&&( y1+ dir2[i] <=m)&&( x1+ dir1[i] >0)&&( y1+ dir2[i] >0) )
{
mat[ x1+ dir1[i] ][ y1+ dir2[i]]=t;
a[++y]=x1+ dir1[i];
b[y]=y1+ dir2[i];
x++; s++;
}
}
t++;
x1=a[x]; y1=b[x];
}
}
//juieta
ok=1;x=1;t=2;y=1;
while(ok)
{
s=x;
while(s<=y)
{ for(i=1;i<=8;i++)
if((x2+ dir1[i]==a[1])&&( y2 + dir2[i]==b[1]))
{
// fprintf(g,"%hd %hd %hd\n",t,x2+dir1[i],y2 + dir2[i]);
ok=0;
s=y+1;
break;
//fclose(f);
//fclose(g);
//exit(0);
//g<<x2+ dir1[i]<<" "<<y2 + dir2[i]<<"\n"<<t<<"\n";
}
else
{
if(mat2[ x2+ dir1[i] ][ y2+ dir2[i]]==0)
if(( x2+ dir1[i] <=n)&&( y2+ dir2[i] <=m)&&( x2+ dir1[i] >0)&&( y2+ dir2[i] >0) )
{
mat2[ x2+ dir1[i] ][ y2+ dir2[i]]=t;
c[++y]=x2+ dir1[i];
d[y]=y2+ dir2[i];
x++;s++;
}
}
t++;
x2=c[x]; y2=d[x];
}
}
t--;
for(i=1;i<n;i++)
for(j=1;j<m;j++)
if(mat[i][j]==mat2[i][j]&&mat[i][j]>1)
{fprintf(g,"%hd %hd %hd\n",mat[i][j],i,j);i=n;break;}
fclose(f);
fclose(g);
return 0;}