#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
int Prev[186][186][186];
int Next[186][186][186];
int H, W;
string field[200];
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> H >> W;
    for(int h = 1; h <= H; h++) {
        cin >> field[h];
        field[h] = "&" + field[h];
    }
    /*
    for(int h1 = 1; h1 <= H; h1++) {
        bool hok = true;
        for(int h2 = h1; h2 <= H; h2++) {
            for(int w = 1; w <= W; w++) {
                Prev[h1][h2][w] = w - 1;
                for(int w2 = w; w2 <= W; w2++) {
                    bool ok = true;
                    for(int h3 = h1; h3 <= h2; h3++) {
                        if(field[h3][w2] != field[h1][w]) ok = false;
                    }
                    if(!ok) break;
                    Prev[h1][h2][w] = w2;
                }
                //cerr << h1 << " " << h2 << " " << w << " " << Prev[h1][h2][w] << endl;
            }
        }
    }
    */
    for(int h1 = 1; h1 <= H; h1++) {
        for(int h2 = 1; h2 <= H; h2++) {
            Prev[h1][h2][W] = W;
            for(int h3 = h1; h3 <= h2; h3++) {
                if(field[h1][W] != field[h3][W]) Prev[h1][h2][W] = W - 1;
            }
        }
    }
    for(int w1 = W - 1; w1 >= 1; w1--) {
        for(int h1 = 1; h1 <= H; h1++) {
            bool hok = true;
            for(int h2 = h1; h2 <= H; h2++) {
                if(field[h1][w1] != field[h2][w1]) hok = false;
                if(!hok) {
                    Prev[h1][h2][w1] = w1-1;
                    continue;
                }
                Prev[h1][h2][w1] = w1;
                if(field[h1][w1] == field[h1][w1+1]) {
                    Prev[h1][h2][w1] = Prev[h1][h2][w1+1];
                }
            }
        }
    }
    if(Prev[1][H][1] == W) {
        cout << 0 << endl;
        return 0;
    }
    int ans = 0;
    while(true) {
        ans++;
        //cerr << "----" << ans << "----" << endl;
        for(int w1 = W; w1 >= 1; w1--) {
            for(int h1 = 1; h1 <= H; h1++) {
                int tmp = Prev[h1][h1][w1];
                if(tmp < W) tmp = Prev[h1][h1][tmp+1];
                Next[h1][h1][w1] = tmp;
                int hmed = h1 + 1;
                for(int h2 = h1 + 1; h2 <= H; h2++) {
                    tmp = Prev[h1][h2][w1];
                    if(tmp < W) tmp = Prev[h1][h2][tmp+1];
                    Next[h1][h2][w1] = tmp;
                    while(hmed < h2) {
                        if(Prev[h1][hmed][w1] >= Prev[hmed][h2][w1]) {
                            hmed++;
                            continue;
                        }
                        break;
                    }
                    tmp = min(Prev[h1][hmed-1][w1], Prev[hmed][h2][w1]);
                    Next[h1][h2][w1] = max(Next[h1][h2][w1], tmp);
                    //cerr << h1 << " " << h2 << " " << w1 << " " << Next[h1][h2][w1] << endl;
                }
            }
        }
        if(Next[1][H][1] == W) {
            cout << ans << endl;
            return 0;
        }
        swap(Next, Prev);
        //if(ans >= 3) break;
    }
    return 0;
}


Measured memory_bytes: 50560