#include <iostream>
#include <math.h>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <cstring>
#include <iomanip>
#include <set>
#include <bitset>
#include <unordered_map>
#include <random>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using piii = tuple<int,int,int>;
using piiii = tuple<int,int,int,int>;
#define f first
#define s second
#define endl '\n'
#define all(x) begin(x),end(x)
#define m_p make_pair
pii mov[4] = {{1,0},{0,1},{-1,0},{0,-1}};
int main(){
int n;cin >> n;
int m;cin >> m;
vector<vector<int>> tb(n,vector<int>(m,0));
for(int i{};i < n;i++){
for(int j{};j < m;j++){
char c;cin >> c;
if(c == 'F'){
tb[i][j] = 2;
}
else if(c == 'R'){
tb[i][j] = 3;
}
}
}
queue<pii> q1;//always as current bfs
queue<pii> q2;//always as future bfs
q1.emplace(0,0);
int i = 1;
while(true){
if(i&1){
if(q1.empty()) break;
int c = tb[q1.front().f][q1.front().s];
//cout << c << endl;
while(!q1.empty()){
auto [i,j] = q1.front();q1.pop();
for(auto [a,b]:mov){
if(i+a < 0 || i+a >= n) continue;
if(j+b < 0 || j+b >= m) continue;
if(tb[i+a][j+b] < 2) continue;
if(tb[i+a][j+b] == c){
tb[i+a][j+b] = 1;
q1.emplace(i+a,j+b);
}
else if(tb[i+a][j+b]){
tb[i+a][j+b] = 1;
q2.emplace(i+a,j+b);
}
}
}
}
else{
if(q2.empty()) break;
int c = tb[q2.front().f][q2.front().s];
while(!q2.empty()){
auto [i,j] = q2.front();q2.pop();
for(auto [a,b]:mov){
if(i+a < 0 || i+a >= n) continue;
if(j+b < 0 || j+b >= m) continue;
if(tb[i+a][j+b] < 2) continue;
if(tb[i+a][j+b] == c){
tb[i+a][j+b] = 1;
q2.emplace(i+a,j+b);
}
else {
q1.emplace(i+a,j+b);
}
}
}
}
i++;
// for(int i{};i < n;i++){
// for(int j{};j < m;j++){
// cout << tb[i][j] << " ";
// }
// cout << endl;
// }
}
cout << i-1 << endl;
}