#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
using Int = unsigned char;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

const int MX = 186;
int h, w, sum[MX][MX];
vector<string> s;
Int mxh[18][MX][MX][MX], mxw[18][MX][MX][MX];

void chmax(Int &a, Int b){ a = max(a, b); }

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	cin >> h >> w;
	s.resize(h);
	rep(i, h) cin >> s[i];
	rep(i, h) rep(j, w) sum[i + 1][j + 1] = sum[i + 1][j] + sum[i][j + 1] + (s[i][j] == '#') - sum[i][j];
	
	rep(l, w) for(int r = l + 1; r <= w; r++) for(int i = h - 1; i >= 0; i--){
		int cnt = sum[i + 1][r] - sum[i + 1][l] - sum[i][r] + sum[i][l];
		bool same = i + 1 < h && s[i][l] == s[i + 1][l];
		mxh[0][l][r][i] = cnt == r - l || cnt == 0 ? same ? mxh[0][l][r][i + 1] + 1 : 1 : 0;
	}
	rep(u, h) for(int d = u + 1; d <= h; d++) for(int i = w - 1; i >= 0; i--){
		int cnt = sum[d][i + 1] - sum[u][i + 1] - sum[d][i] + sum[u][i];
		bool same = i + 1 < w && s[u][i] == s[u][i + 1];
		mxw[0][u][d][i] = cnt == d - u || cnt == 0 ? same ? mxw[0][u][d][i + 1] + 1 : 1 : 0;
	}
	for(int k = 1; k < 18; k++){
		rep(l, w) for(int r = l + 1; r <= w; r++) rep(i, h){
			Int x = mxh[k - 1][l][r][i];
			Int res = x + mxh[k - 1][l][r][i + x];
			chmax(mxh[k][l][r][i], res);
			chmax(mxw[k][i][i + res][l], r - l);
		}
		rep(u, h) for(int d = u + 1; d <= h; d++) rep(i, w){
			Int x = mxw[k - 1][u][d][i];
			Int res = x + mxw[k - 1][u][d][i + x];
			chmax(mxw[k][u][d][i], res);
			chmax(mxh[k][i][i + res][u], d - u);
		}
		for(int len = w - 1; len >= 1; len--) rep(l, w - len) rep(i, h){
			chmax(mxh[k][l][l + len][i], mxh[k][l][l + len + 1][i]);
		}
		for(int len = h - 1; len >= 1; len--) rep(u, h - len) rep(i, w){
			chmax(mxw[k][u][u + len][i], mxw[k][u][u + len + 1][i]);
		}
	}
	rep(i, 18) if(mxh[i][0][w][0] >= h){
		cout << i << endl;
		return 0;
	}
	assert(0);
}

Measured memory_bytes: 225792