#include <bits/stdc++.h>
#include <type_traits>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,b) FOR(i,0,b)
#define ROF(i,a,b) for(int i=int(b)-1;i>=a;i--)
#define per(i,b) ROF(i,0,b)
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(),x.end()
auto& errStream=cerr;
#ifdef LOCAL
#define cerr (cerr<<"-- line "<<__LINE__<<" -- ")
#else
class CerrDummy{}cerrDummy;
template<class T>
CerrDummy& operator<<(CerrDummy&cd,const T&){
	return cd;
}
using charTDummy=char;
using traitsDummy=char_traits<charTDummy>;
CerrDummy& operator<<(CerrDummy&cd,basic_ostream<charTDummy,traitsDummy>&(basic_ostream<charTDummy,traitsDummy>&)){
	return cd;
}
#define cerr cerrDummy
#endif
#define REACH cerr<<"reached"<<endl
#define DMP(x) cerr<<#x<<":"<<x<<endl
#define ZERO(x) memset(x,0,sizeof(x))
#define ONE(x) memset(x,-1,sizeof(x))
#define fs first
#define sc second

template<class T> using V=vector<T>;
template<class T> using VV=V<V<T>>;

using pi=pair<int,int>;
using vi=vector<int>;
using ld=long double;

template<class T,class U>
ostream& operator<<(ostream& os,const pair<T,U>& p){
	os<<"("<<p.first<<","<<p.second<<")";
	return os;
}

template<class T>
ostream& operator <<(ostream& os,const vector<T>& v){
	os<<"{";
	rep(i,(int)v.size()){
		if(i)os<<",";
		os<<v[i];
	}
	os<<"}";
	return os;
}

template<int i,class T>
void print_tuple(ostream&,const T&){
}

template<int i,class T,class H,class ...Args>
void print_tuple(ostream&os,const T&t){
	if(i)os<<",";
	os<<get<i>(t);
	print_tuple<i+1,T,Args...>(os,t);
}

template<class ...Args>
ostream& operator<<(ostream&os,const tuple<Args...>&t){
	os<<"(";
	print_tuple<0,tuple<Args...>,Args...>(os,t);
	return os<<")";
}

ll read(){
	ll i;
	scanf("%lld",&i);
	return i;
}

void printSpace(){
	printf(" ");
}

void printEoln(){
	printf("\n");
}

void print(ll x,int suc=1){
	printf("%lld",x);
	if(suc==1)
		printEoln();
	if(suc==2)
		printSpace();
}

template<class T>
void print(const vector<T>&v,int suc=1){
	rep(i,v.size())
		print(v[i],i==int(v.size())-1?suc:2);
}

string readString(){
	static char buf[3341000];
	scanf("%s",buf);
	return string(buf);
}

char* readCharArray(){
	static char buf[3341000];
	static int bufUsed=0;
	char* ret=buf+bufUsed;
	scanf("%s",ret);
	bufUsed+=strlen(ret)+1;
	return ret;
}

template<class T,class U>
void chmax(T& a,U b){
	if(a<b)
		a=b;
}

template<class T,class U>
void chmin(T& a,U b){
	if(b<a)
		a=b;
}

template<class T>
T Sq(const T& t){
	return t*t;
}

#define CAPITAL
void Yes(bool ex=true){
	#ifdef CAPITAL
	cout<<"YES"<<endl;
	#else
	cout<<"Yes"<<endl;
	#endif
	if(ex)exit(0);
}
void No(bool ex=true){
	#ifdef CAPITAL
	cout<<"NO"<<endl;
	#else
	cout<<"No"<<endl;
	#endif
	if(ex)exit(0);
}

const ll infLL=LLONG_MAX/3;

#ifdef int
const int inf=infLL;
#else
const int inf=INT_MAX/2-100;
#endif

constexpr ll TEN(int n){
	return n==0?1:TEN(n-1)*10;
}

template<class T>
vector<T> Uniqued(const vector<T>&vv){
	auto v(vv);
	sort(all(v));
	v.erase(unique(all(v)),v.end());
	return v;
}
template<class T>
void MakeUniqued(vector<T>&v){
	sort(all(v));
	v.erase(unique(all(v)),v.end());
}

const int S=200;
int dp[2][S][S][S];
int b[S][S][S];

signed main(){
	int h=read(),w=read();
	V<string> s;
	rep(i,h)s.pb(readString());
	int cur=0;
	rep(i,h)rep(j,w){
		int k=j;
		while(k<w&&s[i][k]==s[i][j])k++;
		dp[cur][i][i][j]=k;
	}
	rep(i,h)FOR(j,i+1,h)rep(k,w){
		dp[cur][i][j][k]=min(dp[cur][i][j-1][k],dp[cur][j][j][k]);
		if(s[j-1][k]!=s[j][k])dp[cur][i][j][k]=k;
	}
	rep(i,h)FOR(j,i,h)
		dp[cur][i][j][w]=w;
	int ans=0;
	while(dp[cur][0][h-1][0]<w){
		ans++;
		int nx=cur^1;
		rep(i,h)FOR(j,i,h)rep(k,w+1)
			dp[nx][i][j][k]=dp[cur][i][j][dp[cur][i][j][k]];
		rep(i,w)FOR(j,i,w)rep(k,h+1)
			b[i][j][k]=k;
		rep(i,h)FOR(j,i,h)rep(k,w)if(dp[cur][i][j][k]>k)
			chmax(b[k][dp[cur][i][j][k]-1][i],j+1);
		per(i,w)ROF(j,i+1,w)rep(k,h+1){
			chmax(b[i+1][j][k],b[i][j][k]);
			chmax(b[i][j-1][k],b[i][j][k]);
		}
		rep(i,w)FOR(j,i,w)rep(k,h){
			int z=b[i][j][b[i][j][k]];
			if(z>k)
				chmax(dp[nx][k][z-1][i],j+1);
		}
		per(i,h)ROF(j,i+1,h)rep(k,w+1){
			chmax(dp[nx][i+1][j][k],dp[nx][i][j][k]);
			chmax(dp[nx][i][j-1][k],dp[nx][i][j][k]);
		}
		cur=nx;
	}
	print(ans);
}


Measured memory_bytes: 180352