#include <bits/stdc++.h>
using namespace std;
const int maxn = 185;
int p[4][maxn][maxn][maxn];
int g[maxn][maxn];
int main(){
	int n, m; cin >> n >> m;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			char c; cin >> c;
			g[i][j] = c == '.';
			if(i) g[i][j] += g[i - 1][j];
			if(j) g[i][j] += g[i][j - 1];
			if(i && j) g[i][j] -= g[i - 1][j - 1];
		}
	}
	auto sum = [&](int a, int b, int c, int d){
		int ans = g[c][d];
		if(a) ans -= g[a - 1][d];
		if(b) ans -= g[c][b - 1];
		if(a && b) ans += g[a - 1][b - 1];
		return ans;
	};
	auto mono = [&](int a, int b, int c, int d){
		int q = sum(a, b, c, d);
		return q == 0 || q == (c - a + 1) * (d - b + 1);
	};
	for(int i = 0; i < n; i++){
		for(int j = 0; j < m; j++){
			for(int k = i; k < n; k++){
				int lo = j, hi = m - 1, ans = j - 1;
				while(lo <= hi){
					int mid = (lo + hi)>>1;
					if(mono(i, j, k, mid)) ans = mid, lo = mid + 1;
					else hi = mid - 1;
				}
				p[0][i][j][k] = ans;
			}
			for(int k = j; k < m; k++){
				int lo = i, hi = n - 1, ans = i - 1;
				while(lo <= hi){
					int mid = (lo+hi)>>1;
					if(mono(i, j, mid, k)) ans = mid, lo = mid + 1;
					else hi = mid - 1;
				}
				p[1][i][j][k] = ans;
			}
		}
	}
	for(int mvs = 0; ; mvs++){
		if(p[0][0][0][n - 1] >= m - 1){
			cout << mvs << endl;
			return 0;
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				for(int k = i; k < n; k++){
					int now = p[0][i][j][k];
					if(now < m - 1) now = p[0][i][now + 1][k];
					p[2][i][j][k] = now;
				}
				for(int k = j; k < m; k++){
					int now = p[1][i][j][k];
					if(now < n - 1) now = p[1][now + 1][j][k];
					p[3][i][j][k] = now;
				}
			}
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < m; j++){
				for(int k = i; k < n; k++){
					int now = p[2][i][j][k];
					while(now + 1 < m && p[3][i][j][now + 1] >= k) now++;
					p[0][i][j][k] = now;
				}
				for(int k = j; k < m; k++){
					int now = p[3][i][j][k];
					while(now + 1 < n && p[2][i][j][now + 1] >= k) now++;
					p[1][i][j][k] = now;	
				}
			}
		}
	}
	return 0;
}
