#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int nx = 4005;
ll dist[nx][nx];
int H, W;
int grid[nx][nx];
int xx[4] = {0, -1, 0, 1};
int yy[4] = {1, 0, -1, 0};
queue<tuple<ll,int,int>> q;
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> H >> W;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
char c; cin >> c;
dist[i][j] = 1e18;
if (c == 'R') grid[i][j] = 1;
else if (c == 'F') grid[i][j] = 0;
else if (c == '.') grid[i][j] = -1;
}
}
dist[1][1] = 1;
q.emplace(1, 1, 1);
while (!q.empty()) {
auto [w, y, x] = q.front();
q.pop();
if (dist[y][x] < w) continue;
for (int i = 0; i < 4; i++) {
int ny = y + yy[i], nx = x + xx[i];
if (ny < 1 || ny > H || nx < 1 || nx > W) continue;
if (grid[ny][nx] == -1) continue;
if (grid[ny][nx] == grid[y][x]) {
if (dist[ny][nx] > w) {
dist[ny][nx] = w;
q.emplace(w, ny, nx);
}
continue;
}
if (dist[ny][nx] > w + 1) {
dist[ny][nx] = w + 1;
q.emplace(w + 1, ny, nx);
}
}
}
ll ans = 0;
for (int i = 1; i <= H; i++) {
for (int j = 1; j <= W; j++) {
if (grid[i][j] == -1) continue;
ans = max(ans, dist[i][j]);
}
}
cout << ans;
}