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

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<int, ii> iii;
typedef pair<ii, int> ri3;
#define mp make_pair
#define pb push_back
#define fi first
#define sc second
#define SZ(x) (int)(x).size()
#define ALL(x) begin(x), end(x) 
#define REP(i, n) for (int i = 0; i < n; ++i) 
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define RFOR(i, a, b) for (int i = a; i >= b; --i)

int main() {
    //freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0);

    int h, w; cin >> h >> w;
    char g[h+1][w+1] = {0};

    int L[h+1][w+1];
    int U[h+1][w+1];
    memset(L, 0, sizeof L);
    memset(U, 0, sizeof U);
    FOR(i,1,h) {
        cin >> (g[i]+1);

        FOR(j,1,w) {
            L[i][j] = (g[i][j] == g[i][j-1] ? L[i][j-1] + 1 : 1);
            U[i][j] = (g[i][j] == g[i-1][j] ? U[i-1][j] + 1 : 1);
        }
    }

    int LL[h+1][w+1][h+1][2];
    int UU[h+1][w+1][w+1][2];
    memset(LL, 0, sizeof LL);
    memset(UU, 0, sizeof UU);

    FOR(i,1,h) FOR(j,1,w) {
        int mn = 1e9;
        FOR(x,i,h) {
            if (g[i][j] != g[x][j]) break;
            mn = min(mn, L[x][j]);
            LL[i][j][x][0] = mn;
        }

        mn = 1e9;
        FOR(x,j,w) {
            if (g[i][j] != g[i][x]) break;
            mn = min(mn, U[i][x]);
            UU[i][j][x][0] = mn;
        }
    }

    //cout << UU[h][1][1][0] << " " << UU[h][2][3][0] << endl;
    FOR(ans,0,20){
        int cur = ans&1, nxt = cur^1;
        if (LL[1][w][h][cur] == w or UU[h][1][w][cur] == h) return !(cout << ans << '\n');

        //cout << ans << " :: " << LL[1][w][h][cur] << " " << UU[h][1][w][cur] << endl;

        FOR(i,1,h) FOR(j,1,w) {
            FOR(x,i,h) {
                LL[i][j][x][nxt] = LL[i][j][x][cur];
                LL[i][j][x][nxt] += LL[i][j-LL[i][j][x][cur]][x][cur];

                int lo = i, hi = x;
                while (lo < hi) {
                    int mid = lo+hi >> 1;
                    if (LL[i][j][mid][cur] < LL[mid+1][j][x][cur])
                        LL[i][j][x][nxt] = max(LL[i][j][x][nxt], LL[i][j][mid][cur]), hi = mid;
                    else
                        LL[i][j][x][nxt] = max(LL[i][j][x][nxt], LL[mid+1][j][x][cur]), lo = mid+1;
                }
                //FOR(y,i,x-1) {
                //    LL[i][j][x][nxt] = max(
                //            LL[i][j][x][nxt],
                //            min(LL[i][j][y][cur], LL[y+1][j][x][cur]));
                //}
            }
            FOR(x,j,w) {
                UU[i][j][x][nxt] = UU[i][j][x][cur];
                UU[i][j][x][nxt] += UU[i-UU[i][j][x][cur]][j][x][cur];
                
                int lo = j, hi = x;
                while (lo < hi) {
                    int mid = lo+hi >> 1;
                    if (UU[i][j][mid][cur] < UU[i][mid+1][x][cur])
                        UU[i][j][x][nxt] = max(UU[i][j][x][nxt], UU[i][j][mid][cur]), hi = mid;
                    else
                        UU[i][j][x][nxt] = max(UU[i][j][x][nxt], UU[i][mid+1][x][cur]), lo = mid+1;
                }
                //FOR(y,j,x-1) {
                //    UU[i][j][x][nxt] = max(
                //            UU[i][j][x][nxt],
                //            min(UU[i][j][y][cur], UU[i][y+1][x][cur]));
                //}
            }
        }
    }
}



Measured memory_bytes: 101120