// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <iomanip>
#define dibs reserve
#define OVER9000 1234567890
#define patkan 9
#define tisic 47
#define soclose 1e-9
#define pi 3.1415926535898
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define chocolate win
#define ff first
#define ss second
#define abs(x) ((x < 0)?-(x):(x))
#define uint unsigned int
// mylittlepony
using namespace std;
int main() {
// freopen("skicourse.in","r",stdin);
// freopen("skicourse.out","w",stdout);
int N,M;
scanf(" %d %d",&N,&M);
vector< vector<bool> > isS, isR, isC;
isS.resize(N, vector<bool>(M,false));
isR.resize(N, vector<bool>(M,false));
isC.resize(N, vector<bool>(M,false));
char c[142];
for(int i =0; i < N; i++) {
scanf(" %s",c);
for(int j =0; j < M; j++) {
if(c[j] == 'R') isR[i][j] =true;
else isS[i][j] =true;}}
vector< vector<int> > sumS, sumR, sumC;
sumS.resize(N+1, vector<int>(M+1,0));
sumR.resize(N+1, vector<int>(M+1,0));
sumC.resize(N+1, vector<int>(M+1,0));
for(int i =0; i < N; i++) for(int j =0; j < M; j++) {
sumS[i+1][j+1] =(int)(isS[i][j])+sumS[i+1][j]+sumS[i][j+1]-sumS[i][j];
sumR[i+1][j+1] =(int)(isR[i][j])+sumR[i+1][j]+sumR[i][j+1]-sumR[i][j];}
int B =min(N,M);
set< pair<int,int> > F;
for(int i =0; i < N; i++) for(int j =0; j < M; j++) F.insert(make_pair(i,j));
while(sumC[N][M] < N*M) {
bool b =false;
vector< pair<int,int> > D;
for(set< pair<int,int> >::iterator it =F.begin(); it != F.end(); it++) {
if(it->ff+B > N || it->ss+B > M) continue;
// da sa vyfarbit, je v nom daco nove?
int s =sumC[it->ff+B][it->ss+B]+sumC[it->ff][it->ss]-sumC[it->ff+B][it->ss]-sumC[it->ff][it->ss+B];
if(s == B*B) {
D.push_back(*it);
continue;}
int sS =sumS[it->ff+B][it->ss+B]+sumS[it->ff][it->ss]-sumS[it->ff+B][it->ss]-sumS[it->ff][it->ss+B];
int sR =sumR[it->ff+B][it->ss+B]+sumR[it->ff][it->ss]-sumR[it->ff+B][it->ss]-sumR[it->ff][it->ss+B];
if(max(sR,sS) < B*B) continue;
D.push_back(*it);
for(int i =0; i < B; i++) for(int j =0; j < B; j++) if(isS[it->ff+i][it->ss+j]^isR[it->ff+i][it->ss+j]) {
if(!isS[it->ff+i][it->ss+j])
for(int k =it->ff+i+1; k <= N; k++)
for(int l =it->ss+j+1; l <= M; l++)
sumS[k][l]++;
else for(int k =it->ff+i+1; k <= N; k++)
for(int l =it->ss+j+1; l <= M; l++)
sumR[k][l]++;
for(int k =it->ff+i+1; k <= N; k++)
for(int l =it->ss+j+1; l <= M; l++)
sumC[k][l]++;
isS[it->ff+i][it->ss+j] =isR[it->ff+i][it->ss+j] =true;}
b =true;}
if(!b) B--;
for(int i =0; i < D.size(); i++) F.erase(D[i]);}
printf("%d\n",B);
return 0;}
// look at my code
// my code is amazing
Ly8gaW9zdHJlYW0gaXMgdG9vIG1haW5zdHJlYW0KI2luY2x1ZGUgPGNzdGRpbz4KLy8gYml0Y2ggcGxlYXNlCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHNldD4KI2luY2x1ZGUgPG1hcD4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPHF1ZXVlPgojaW5jbHVkZSA8c3RhY2s+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPGlvbWFuaXA+CiNkZWZpbmUgZGlicyByZXNlcnZlCiNkZWZpbmUgT1ZFUjkwMDAgMTIzNDU2Nzg5MAojZGVmaW5lIHBhdGthbiA5CiNkZWZpbmUgdGlzaWMgNDcKI2RlZmluZSBzb2Nsb3NlIDFlLTkKI2RlZmluZSBwa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