#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
vector<int> adj[10];
bool vis[10];
int arx[4] = {1,-1,0,0};
int ary[4] = {0,0,-1,1};
int main(){
/*#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif*/
int h,w;
cin>>h>>w;
char grid[h][w];
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
cin>>grid[i][j];
}
}
int ans = 0;
bool test = true;
while(test){
test=false;
ans++;
queue<pair<int,int>> q;
q.push({0,0});
char start = grid[0][0];
bool vis[h][w];
memset(vis,0,sizeof vis);
vis[0][0]=1;
while(!q.empty()){
pair<int,int> cur = q.front();
q.pop();
for(int i =0;i<4;i++){
int x = cur.fi + arx[i];
int y = cur.se + ary[i];
if(x>=0 && y>=0 && x < h && y < w){
if(vis[x][y]==0 && grid[x][y] == start){
q.push({x,y});
vis[x][y]=1;
}else if(vis[x][y] == 0 && grid[x][y] != '.'){
test=true;
}
}
}
}
for (int i = 0; i < h; ++i)
{
for (int j = 0; j < w; ++j)
{
if(vis[i][j]) grid[i][j] = (start == 'F' ? 'R' : 'F');
}
}
}
cout <<ans<<endl;
}