// romjul.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
//Compiler version g++ 6.3.0
void lee(int mat[101][101], int i, int j, int n, int m, int cnt,int ifin,int jfin ) {
mat[i][j] = cnt;
if (i == ifin && j == jfin) {
//dosomething
}
else {
if (mat[i + 1][j] == 0 || mat[i + 1][j] > cnt + 1) {
lee(mat, i + 1, j, n, m, cnt + 1,ifin,jfin);
}
if (mat[i][j + 1] == 0 || mat[i][j + 1] > cnt + 1) {
lee(mat, i, j + 1, n, m, cnt + 1, ifin, jfin);
}
if (mat[i - 1][j] == 0 || mat[i - 1][j] > cnt + 1) {
lee(mat, i - 1, j, n, m, cnt + 1, ifin, jfin);
}
if (mat[i][j - 1] == 0 || mat[i][j - 1] > cnt + 1) {
lee(mat, i, j - 1, n, m, cnt + 1, ifin, jfin);
}
////
if (mat[i + 1][j + 1] == 0 || mat[i + 1][j + 1] > cnt + 1) {
lee(mat, i + 1, j + 1, n, m, cnt + 1, ifin, jfin);
}
if (mat[i + 1][j - 1] == 0 || mat[i + 1][j - 1] > cnt + 1) {
lee(mat, i + 1, j - 1, n, m, cnt + 1, ifin, jfin);
}
if (mat[i - 1][j + 1] == 0 || mat[i - 1][j + 1] > cnt + 1) {
lee(mat, i - 1, j + 1, n, m, cnt + 1, ifin, jfin);
}
if (mat[i - 1][j - 1] == 0 || mat[i - 1][j - 1] > cnt + 1) {
lee(mat, i - 1, j - 1, n, m, cnt + 1, ifin, jfin);
}
}
}
void bord(int mat[101][101], int n, int m) {
for (int i = 0; i <= n + 1; i++) {
mat[i][0] = mat[i][m + 1] = -1;
}
for (int i = 0; i <= m + 1; i++) {
mat[0][i] = mat[n + 1][i] = -1;
}
}
void wr(int mat[101][101], int n, int m) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
//int mat[101][101];
int jul[101][101];
int rom[101][101];
char vec[1000];
int main()
{
int n, m, ij, ir, jj, jr;
char c;
in >> n >> m;
in.get();
for (int i = 1; i <= n; i++) {
//for(int j=1;j<=m;j++){
in.getline(vec, 300);
for (int j = 0; j < strlen(vec); j++) {
c = vec[j];
if (vec[j] == 32) {
//mat[i][j+1]=0;
rom[i][j + 1] = 0;
jul[i][j + 1] = 0;
}
else if (vec[j] == 'R') {
ir = i;
jr = j + 1;
//mat[i][j+1]=-2;
jul[i][j + 1] = -1;
rom[i][j + 1] = -2;
}
else if (vec[j] == 'J') {
ij = i;
jj = j + 1;
jul[i][j + 1] = -2;
rom[i][j + 1] = -1;
}
else if (vec[j] == 'X') {
//mat[i][j+1]=-1;
jul[i][j + 1] = -1;
rom[i][j + 1] = -1;
}
}
//}
}
//bord(mat,n,m);
/*
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(i==ir&&j==jr){
rom[i][j]=-2;
jul[i][j]=-1;
}else if(i==ij&&j==jj){
rom[i][j]=-1;
jul[i][j]=-2;
}else{
rom[i][j]=jul[i][j]=mat[i][j];
}
}
//cout<<endl;
}*/
bord(rom, n, m);
bord(jul, n, m);
lee(rom, ir, jr, n, m, 1,ij,jj);
rom[ir][jr] = 1;
lee(jul, ij, jj, n, m, 1,ir,jr);
jul[ij][jj] = 1;
//wr(rom,n,m);
//wr(jul,n,m);
int value, pozii, pozjj;
int mini = 200000;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (rom[i][j] > 0 && jul[i][j] > 0) {
if (rom[i][j] == jul[i][j]) {
if (rom[i][j] < mini) {
mini = rom[i][j];
value = rom[i][j];
pozii = i;
pozjj = j;
}
}
}
}
}
out << value << " " << pozii << " " << pozjj;
return 0;
//cout << "Hello, Dcoder!";
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln file