#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repe(i, a, b) for (int i = a; i <= b; i++)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define F first
#define S second
#define pb push_back
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
ll mod = 1000000007;
template<typename T>
void printv(vector<T> x){
for (T i : x) cout << i << " ";
cout << endl;
}
int dirx[] = {1, 0, -1, 0};
int diry[] = {0, 1, 0, -1};
void solve() {
int n, m;
cin >> n >> m;
vector<vi> arr(n, vi(m));
rep(i, 0, n) {
string str;
cin >> str;
rep(j, 0, m) {
if (str[j] == '.') arr[i][j] = 0;
else if (str[j] == 'R') arr[i][j] = 1;
else arr[i][j] = 2;
}
}
deque<pii> bfs;
vector<vi> dist(n, vi(m, -1));
bfs.pb({0, 0});
dist[0][0] = 0;
int maxdist = 0;
while (!bfs.empty()) {
pii p = bfs.front();
bfs.pop_front();
rep(i, 0, 4) {
if (p.F + dirx[i] < n && p.F + dirx[i] >= 0) {
if (p.S + diry[i] < m && p.S + diry[i] >= 0) {
if (dist[p.F + dirx[i]][p.S + diry[i]] != -1) continue;
if (arr[p.F + dirx[i]][p.S + diry[i]] == 0) continue;
else {
if (arr[p.F][p.S] == arr[p.F + dirx[i]][p.S + diry[i]]) {
dist[p.F + dirx[i]][p.S + diry[i]] = dist[p.F][p.S];
bfs.push_front({p.F + dirx[i], p.S + diry[i]});
} else {
dist[p.F + dirx[i]][p.S + diry[i]] = dist[p.F][p.S] + 1;
bfs.pb({p.F + dirx[i], p.S + diry[i]});
}
maxdist = max(maxdist, dist[p.F + dirx[i]][p.S + diry[i]]);
}
}
}
}
}
cout << maxdist + 1 << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin.exceptions(cin.failbit);
// freopen("file.in", "r", stdin);
// freopen("file.out", "w", stdout);
int tc = 1;
// cin >> tc;
for (int t = 1; t <= tc; t++) {
solve();
}
}