#include <map>
#include <set>
#include <ctime>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <bitset>
#include <cstdio>
#include <cctype>
#include <string>
#include <numeric>
#include <cstring>
#include <cassert>
#include <climits>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std ;
#define int long long
#define rep(i, a, b) for (ll i = (a); i <= (b); ++i)
#define per(i, a, b) for (ll i = (a); i >= (b); --i)
#define loop(it, v) for (auto it = v.begin(); it != v.end(); it++)
#define cont(i, x) for (ll i = head[x]; i; i = edge[i].nex)
#define clr(a) memset(a, 0, sizeof(a))
#define ass(a, cnt) memset(a, cnt, sizeof(a))
#define cop(a, b) memcpy(a, b, sizeof(a))
#define lowbit(x) (x & -x)
#define all(x) x.begin(), x.end()
#define SC(t, x) static_cast <t> (x)
#define ub upper_bound
#define lb lower_bound
#define pqueue priority_queue
#define mp make_pair
#define pb push_back
#define pof pop_front
#define pob pop_back
#define fi first
#define se second
#define y1 y1_
#define Pi acos(-1.0)
#define iv inline void
#define enter putchar('\n')
#define siz(x) ((ll)x.size())
#define file(x) freopen(x".in", "r", stdin),freopen(x".out", "w", stdout)
typedef double db ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair <ll, ll> pii ;
typedef vector <ll> vi ;
typedef vector <pii> vii ;
typedef queue <ll> qi ;
typedef queue <pii> qii ;
typedef set <ll> si ;
typedef map <ll, ll> mii ;
typedef map <string, ll> msi ;
const ll maxn = 200 ;
const ll inf = 0x3f3f3f3f ;
const ll iinf = 1 << 30 ;
const ll linf = 2e18 ;
const ll mod = 998244353 ;
const double eps = 1e-7 ;
template <class T = ll> T chmin(T &a, T b) { return a = min(a, b);}
template <class T = ll> T chmax(T &a, T b) { return a = max(a, b);}
template <class T = ll> T read()
{
	T f = 1, a = 0;
	char ch = getchar() ;
	while(!isdigit(ch)) { if(ch == '-') f = -1 ; ch = getchar() ; }
	while(isdigit(ch)) { a = (a << 3) + (a << 1) + ch - '0' ; ch = getchar() ; }
	return a * f ;
}

ll h, w;

char s[maxn];

ll a[maxn][maxn], pre[maxn][maxn];

ll f[maxn][maxn][maxn][17], g[maxn][maxn][maxn];

signed main()
{
	h = read(), w = read();
	rep(i, 1, h)
	{
		scanf("%s", s + 1);
		rep(j, 1, w) a[i][j] = s[j] == '#', pre[i][j] = pre[i - 1][j] + a[i][j];
	}
	rep(i, 1, h) rep(j, i, h)
	{
		f[i][j][w + 1][0] = w;
		ll last = -1;
		per(l, w, 1)
		{
			// printf("%lld %lld %lld :\n", i, j, l);
			f[i][j][l][0] = l - 1;
			if(pre[j][l] - pre[i - 1][l] != 0 && pre[j][l] - pre[i - 1][l] != j - i + 1)
			{
				last = 2;
				// printf("%lld\n", f[i][j][l][0]);
				continue;
			}
			ll now = (pre[j][l] - pre[i - 1][l]) == 0;
			f[i][j][l][0] = l;
			if(last == -1 || last == now) chmax(f[i][j][l][0], f[i][j][l + 1][0]);//, puts("SAME");
			// printf("%lld %lld\n", now, last);
			last = now;
			// printf("%lld\n", f[i][j][l][0]);
		}
	}
	rep(i, 0, 16)
	{
		per(l, w, 1)
		{
			rep(u, 1, h)
			{
				ll pos = u;
				rep(v, u, h)
				{
					f[u][v][w + 1][i] = w;
					chmax(f[u][v][l][i], l - 1);
					if(i && v != u)
					{
						while(pos < v - 1 && f[u][pos][l][i - 1] > f[pos + 1][v][l][i - 1]) pos ++;
						// printf("pos : %lld\n", pos);
						chmax(f[u][v][l][i], min(f[u][pos][l][i - 1], f[pos + 1][v][l][i - 1]));
						if(pos != u) chmax(f[u][v][l][i], min(f[u][pos - 1][l][i - 1], f[pos][v][l][i - 1]));//, printf("pos : %lld\n", pos);
					}
					chmax(f[u][v][l][i + 1], f[u][v][f[u][v][l][i] + 1][i]);
					chmax(f[u][v][l][i + 1], f[u][v][l][i]);
					// printf("%lld %lld %lld %lld : %lld\n", u, v, l, i, f[u][v][l][i]);
				}
			}
		}
		if(f[1][h][1][i] >= w) return printf("%lld\n", i), 0;
	}
	return 0;
}

Measured memory_bytes: 461740