#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

int H,W;
V<string> s;
const int X = 187;
int lrd2u[2][X][X][X];
int dul2r[2][X][X][X];

int A[X][X];
bool allEqual(int d,int u,int l,int r){
	int c = A[u][r] - A[u][l] - A[d][r] + A[d][l];
	return c == 0 || c == (u-d)*(r-l);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);
	
	cin >> H >> W;
	s.resize(H);
	rep(i,H) cin >> s[i];
	rep(i,H) rep(j,W){
		if(s[i][j] == '#') A[i+1][j+1] = 1;
	}
	rep(i,H+1) rep(j,W) A[i][j+1] += A[i][j];
	rep(i,H) rep(j,W+1) A[i+1][j] += A[i][j];



	rep(l,W) for(int r=l+1;r<=W;r++){
		lrd2u[0][l][r][H] = H;
		for(int d=H-1;d>=0;d--){
			lrd2u[0][l][r][d] = d;
			if(allEqual(d,d+1,l,r)){
				lrd2u[0][l][r][d] = d + 1;
				if(d != H-1 && s[d][l] == s[d+1][l]) lrd2u[0][l][r][d] = lrd2u[0][l][r][d+1];
			}
		}
	}
	rep(d,H) for(int u=d+1;u<=H;u++){
		dul2r[0][d][u][W] = W;
		for(int l=W-1;l>=0;l--){
			dul2r[0][d][u][l] = l;
			if(allEqual(d,u,l,l+1)){
				dul2r[0][d][u][l] = l+1;
				if(l != W-1 && s[d][l] == s[d][l+1]) dul2r[0][d][u][l] = dul2r[0][d][u][l+1];
			}
		}
	}
	rep(t,100){

		// cerr << " -----------turn =  " << t << endl;
		// rep(l,W) for(int r=l+1;r<=W;r++) rep(d,H){
		// 	cerr << "lrd2u["<<l<<"]["<<r<<"]["<<d<<"] = " << lrd2u[t&1][l][r][d] << endl;
		// }
		// rep(d,H) for(int u=d+1;u<=H;u++) rep(l,W){
		// 	cerr << "dul2r["<<d<<"]["<<u<<"]["<<l<<"] = " << dul2r[t&1][d][u][l] << endl;
		// }

		if(lrd2u[t&1][0][W][0] == H){
			cout << t << endl;
			return 0;
		}

		rep(l,W) for(int r=l+1;r<=W;r++){
			rep(d,H+1){
				lrd2u[1-(t&1)][l][r][d] = lrd2u[t&1][l][r][lrd2u[t&1][l][r][d]];
			}
		}
		rep(d,H) for(int u=d+1;u<=H;u++){
			rep(l,W+1){
				dul2r[1-(t&1)][d][u][l] = dul2r[t&1][d][u][dul2r[t&1][d][u][l]];
			}
		}
		rep(l,W) for(int r=l+1;r<=W;r++){
			rep(d,H+1){
				int u = lrd2u[1-(t&1)][l][r][d];
				chmax(dul2r[1-(t&1)][d][u][l],r);
			}
		}
		rep(d,H) for(int u=d+1;u<=H;u++){
			rep(l,W+1){
				int r = dul2r[1-(t&1)][d][u][l];
				chmax(lrd2u[1-(t&1)][l][r][d],u);
			}
		}
		for(int len=W;len>0;len--){
			rep(l,W){
				int r = l + len;
				if(r > W) break;
				rep(d,H+1){
					chmax(lrd2u[1-(t&1)][l+1][r][d],lrd2u[1-(t&1)][l][r][d]);
					chmax(lrd2u[1-(t&1)][l][r-1][d],lrd2u[1-(t&1)][l][r][d]);
				}
			}
		}
		for(int len=H;len>0;len--){
			rep(d,H){
				int u = d + len;
				if(u > H) break;
				rep(l,W+1){
					chmax(dul2r[1-(t&1)][d+1][u][l],dul2r[1-(t&1)][d][u][l]);
					chmax(dul2r[1-(t&1)][d][u-1][l],dul2r[1-(t&1)][d][u][l]);
				}
			}
		}
		
	}
}


Measured memory_bytes: 100736