#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define db double
#define pint pair<int,int>
#define mk(x,y) make_pair(x,y)
#define fir first
#define sec second
#define Rep(x,y,z) for(int x=y;x<=z;++x)
#define Red(x,y,z) for(int x=y;x>=z;--x)
using namespace std;
const int MAXN=200;
char buf[1<<12],*p1=buf,*p2=buf,nc;int ny;
//inline char gc() {return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<12,stdin),p1==p2)?EOF:*p1++;}
inline char gc(){return getchar();}
inline int read(){
	int x=0;ny=1;while(nc=gc(),(nc<48||nc>57)&&nc!=EOF)if(nc==45)ny=-1;if(nc<0)return nc;
	x=nc-48;while(nc=gc(),47<nc&&nc<58&&nc!=EOF)x=(x<<3)+(x<<1)+(nc^48);return x*ny;
}int n,m,row[MAXN][MAXN],col[MAXN][MAXN],F[MAXN][MAXN][MAXN],G[MAXN][MAXN][MAXN],ans,cur1[MAXN][MAXN][MAXN],cur2[MAXN][MAXN][MAXN];char s[MAXN][MAXN];
inline void pre(){
	Rep(i,1,n)Red(j,m,1)row[i][j]=(s[i][j]==s[i][j+1]?row[i][j+1]:0)+1;
	Red(i,n,1)Rep(j,1,m)col[i][j]=(s[i][j]==s[i+1][j]?col[i+1][j]:0)+1;
	Rep(i,1,n)Rep(j,1,m){F[i][j][j]=col[i][j];Rep(k,j+1,m)F[i][j][k]=min(F[i][j][k-1],s[i][j]==s[i][k]?col[i][k]:0);}
	Rep(i,1,m)Rep(j,1,n){G[i][j][j]=row[j][i];Rep(k,j+1,n)G[i][j][k]=min(G[i][j][k-1],s[j][i]==s[k][i]?row[k][i]:0);}
}inline void Dp(){
	Rep(i,1,n)Rep(j,1,m)Rep(k,j,m)F[i][j][k]+=F[i+F[i][j][k]][j][k],(F[i][j][k]?cur1[j][i][i+F[i][j][k]-1]=max(cur1[j][i][i+F[i][j][k]-1],k-j+1):0);
	Rep(i,1,m)Rep(j,1,n)Rep(k,j,n)G[i][j][k]+=G[i+G[i][j][k]][j][k],(G[i][j][k]?cur2[j][i][i+G[i][j][k]-1]=max(cur2[j][i][i+G[i][j][k]-1],k-j+1):0);
	Rep(i,1,n)Rep(j,1,m)Red(k,m,j)F[i][j][k]=max(F[i][j][k],max(F[i][j][k+1],cur2[i][j][k]));
	Rep(i,1,m)Rep(j,1,n)Red(k,n,j)G[i][j][k]=max(G[i][j][k],max(G[i][j][k+1],cur1[i][j][k]));
}
int main(){
//	freopen("std.in","r",stdin);
//	freopen("std.out","w",stdout);
	n=read(),m=read();
	Rep(i,1,n)scanf("%s",s[i]+1);
	for(pre(),ans=0;F[1][1][m]<n;ans++,Dp());cout<<ans<<'\n';
	return 0;
}


