#include <bits/stdc++.h>
using namespace std;

namespace TYC
{
	typedef long long ll;

	const int N = 187, M = 17;

	int H, W, A[N][N], F[M][N][N][N], down[N][N], L[N][N][N], R[N][N][N];
	char str[N];

	inline int read()
	{
		int x = 0, f = 0, ch = getchar();
		while (!isdigit(ch))
			f |= (ch == '-'), ch = getchar();
		while (isdigit(ch))
			x = x * 10 + ch - '0', ch = getchar();
		return f ? -x : x;
	}

	void work()
	{
		H = read(), W = read();
		for (int i = 1; i <= H; i++)
		{
			scanf("%s", str + 1);
			for (int j = 1; j <= W; j++)
				A[i][j] = str[j] == '#';
		}
		for (int i = H; i; i--)
			for (int j = 1; j <= W; j++)
				down[i][j] = (i == H || A[i][j] != A[i + 1][j]) ? i : down[i + 1][j];
		for (int i = H; i; i--)
			for (int l = 1; l <= W; l++)
				for (int r = l, v = INT_MAX; r <= W && A[i][l] == A[i][r]; r++)
					F[0][i][l][r] = v = min(v, down[i][r]);
		if (F[0][1][1][W] >= H)
			return void(puts("0"));
		for (int k = 0; k + 1 < M; k++)
		{
			memset(L, 0x3f, sizeof(L));
			memset(R, 0, sizeof(R));
			for (int i = 1; i <= H; i++)
			{
				for (int l = 1; l <= W; l++)
					for (int v = F[k][i][l][l], r = l; ~v; v--)
					{
						while (r < W && F[k][i][l][r + 1] == v)
							++r;
						R[i][l][v] = r;
					}
				for (int r = W; r; r--)
					for (int v = F[k][i][r][r], l = r; ~v; v--)
					{
						while (l > 1 && F[k][i][l - 1][r] == v)
							--l;
						L[i][r][v] = l;
					}
			}
			for (int i = 1; i <= H; i++)
				for (int l = 1; l <= W; l++)
					for (int r = l; r <= W; r++)
					{
						int lv = i, rv = H, mid, ans = 0;
						while (lv <= rv)
						{
							mid = (lv + rv) >> 1;
							if (R[i][l][mid] + 1 >= L[i][r][mid])
								ans = mid, lv = mid + 1;
							else
								rv = mid - 1;	
						}
						F[k + 1][i][l][r] = max(F[k][F[k][i][l][r] + 1][l][r], ans);
					}	
			if (F[k + 1][1][1][W] == H)
				return void(printf("%d\n", k + 1));
		}
	}
}

int main()
{
	TYC::work();
	return 0;
}