// RJ refacut.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;
ifstream in("rj.in");
ofstream out("rj.out");
int iDirectie[9] = { 0, -1, -1, -1, 0, 1, 1, 1, 0 };
int jDirectie[9] = { 0, -1, 0, 1, 1, 1, 0, -1, -1 };
/*struct point {
int i, j;
point() {}
point(int i1, int j1) {
i = i1;
j = j1;
}
}R,J;
*/
pair <int,int> R, J;
//bool vizitatR[105][105], vizitatJ[105][105];
int distR[105][105], distJ[105][105],
di[9] = { -1,-1,-1, 0, 1, 1, 1, 0 },
dj[9] = { -1, 0, 1, 1, 1, 0,-1,-1 },
n, m, distMin = 32767;
char linie[105];
queue <pair<int,int>> q;
int main()
{
in >> n >> m;
in.get();
for (int i = 0; i < n; ++i) {
in.getline(linie, 101);
for (int j = 0; j < m; ++j) {
if (linie[j] == 'R') {
R.first = i;
R.second = j;
distR[i][j] = 1;
//vizitatR[i][j] = true;
continue;
}
if (linie[j] == 'J') {
J.first = i;
J.second = j;
distJ[i][j] = 1;
//vizitatJ[i][j] = true;
continue;
}
if (linie[j] == ' ')
distR[i][j] = distJ[i][j] = 32767;
}
}
//BFS Romeo;
q.push(R);
while (!q.empty()) {
pair <int,int> curent = q.front();
q.pop();
for (int k = 1; k <= 9; ++k) {
pair<int,int> nou(curent.first + iDirectie[k], curent.second + jDirectie[k]);
if (nou.first >= 0 && nou.first < n && nou.second >= 0 && nou.second < m) {
if (distR[nou.first][nou.second] == 32767) {
//vizitatR[nou.first][nou.second] = true;
distR[nou.first][nou.second] = distR[curent.first][curent.second] + 1;
q.push(nou);
}
}
}
}
//BFS Julieta;
q.push(J);
while (!q.empty()) {
pair <int,int> curent = q.front();
q.pop();
for (int k = 1; k <= 9; ++k) {
pair <int,int> nou(curent.first + iDirectie[k], curent.second + jDirectie[k]);
if (nou.first >= 0 && nou.first < n && nou.second >= 0 && nou.second < m) {
if (distJ[nou.first][nou.second] == 32767) {
//vizitatJ[nou.first][nou.second] = true;
distJ[nou.first][nou.second] = distJ[curent.first][curent.second] + 1;
q.push(nou);
}
}
}
}
//gasire punct intalnire
pair<int,int> punctIntalnire;
for (int i = 0; i<n; ++i)
for (int j = 0; j<m; ++j) {
if (distR[i][j] != 0)
if (distR[i][j] == distJ[i][j])
if (distR[i][j] < distMin) {
distMin = distR[i][j];
punctIntalnire.first = i;
punctIntalnire.second = j;
}
}
out << distMin << " " << punctIntalnire.first + 1 << " " << punctIntalnire.second + 1;
cout << distMin << " " << punctIntalnire.first + 1 << " " << punctIntalnire.second + 1;
return 0;
}