#include<bits/stdc++.h>
using namespace std;
//#pragma GCC optimize(3)
const int N = 222;
int f[2][N][N][N];
int n, m;
char mp[N][N];
int main(){
//	freopen("land.in", "r", stdin);
//	freopen("land.out", "w", stdout);
	cin>>n>>m;
	memset(f, -1, sizeof(f));
	for(int i = 1;i <= n; i++){
		scanf("%s", mp[i] + 1);
	}
	for(int i = m;i >= 1; i--){
		for(int l = 1;l <= n; l++){
			for(int r = l;r <= n; r++){
				if(mp[l][i] != mp[r][i])break;
				f[0][l][r][i] = i;
				if(mp[l][i+1] == mp[l][i])f[0][l][r][i] = max(f[0][l][r][i+1], i);
			}
		}
	}
	int fl = 0;
	for(int ans = 0;ans <= 20; ans++){
		if(f[fl][1][n][1] == m){
			printf("%d\n", ans);
			return 0;
		}
		for(int i = 1;i <= m; i++){
			for(int l = 1;l <= n; l++){
				for(int r = n;r >= l; r--){
					if(!f[fl][l][r][i])continue;
					if(f[fl][l][r][i] == m){
						f[fl^1][l][r][i] = m;
						continue;
					}
					int F = f[fl][l][r][i];
					F = max(F, f[fl][l][r][F+1]);
					int L = l, R = r - 1;
					while(L <= R){
						int mid = (L + R) >> 1;
						F = max(F, min(f[fl][l][mid][i], f[fl][mid+1][r][i]));
						if(f[fl][l][mid][i] > f[fl][mid+1][r][i])L = mid + 1;
						else R = mid - 1;
					}
					f[fl^1][l][r][i] = F;
				}
			}
		}
		fl ^= 1;
	}
	return 0;
}