#include <iostream>
#include <vector>
#include <algorithm>
#include <array>
#include <queue>
using namespace std;
#define br '\n'
typedef array<int,2> ii;
const int mxn = 4e3+3;
char g[mxn][mxn];
const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};
int n,m;
char start;
bool place(int x, int y)
{
return (x >= 0 && y >= 0 && x < n && y < m && g[x][y] == start);
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cin>>g[i][j];
}
}
start = g[0][0];
bool two,three; two = three = false;
queue<ii> q;
q.push({0,0});
while(!q.empty())
{
auto [xo,yo] = q.front(); q.pop();
g[xo][yo] = 'x';
for (int i = 0; i < 4; i++){
int x = xo+dx[i];
int y = yo+dy[i];
if ((g[x][y] == 'R' || g[x][y] == 'F') && g[x][y] != start){two = true;}
if (place(x,y))
{
g[x][y] = 'x';
q.push({x,y});
}
}
}
// varrida pra ver se tem 3
for (int i = 0; i < n; i++){
for (int j = 0; j< m; j++){
if (g[i][j] == start){
three = true;
}
}
}
// for (int i = 0; i < n; i++){
// for (int j = 0; j< m; j++){
// cout << g[i][j];
// } cout << br;
// }
if (three) cout << 3 << br;
else if (two) cout << 2 << br;
else cout << 1 << br;
return 0;
}