// <head>
    // #define _GLIBCXX_DEBUG
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    const int INF = 1002003004;
    const ll LINF = 1002003004005006007ll;
    struct preprocess{preprocess()
    {cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
// </head>

// <library>
    
// </library>

int dp1[200][200][200];
int dp2[200][200][200];
char mp[200][200];

int main(){
    int h,w; cin >> h >> w;
    vector<string> mp(h);
    for (int i = 0; i < h; i++){
        cin >> mp[i];
    }
    memset(dp1,-1,sizeof(dp1));
    memset(dp2,-1,sizeof(dp2));
    for (int k = w-1; k >= 0; k--){
        for (int i = 0; i < h; i++){
            for (int j = i; j < h; j++){
                if(mp[i][k]!=mp[j][k]) break;
                else{
                    dp1[i][j][k]=k+1;
                    if(k+1<w&&mp[i][k]==mp[i][k+1]&&dp1[i][j][k+1]!=-1){
                        dp1[i][j][k] = dp1[i][j][k+1];
                    }
                }
            }
        }
    }
    for (int k = h-1; k >= 0; k--){
        for (int i = 0; i < w; i++){
            for (int j = i; j < w; j++){
                if(mp[k][i]!=mp[k][j]) break;
                else{
                    dp2[i][j][k]=k+1;
                    if(k+1<h&&mp[k][i]==mp[k+1][i]&&dp2[i][j][k+1]!=-1){
                        dp2[i][j][k] = dp2[i][j][k+1];
                    }
                }
            }
        }
    }
    for (int comp = 0; comp < 19; comp++){
        if(dp1[0][h-1][0]>=w){
            cout << comp << '\n';
            break;
        }


        for (int i = 0; i < h; i++){
            for (int j = 0; j < h; j++){
                for (int k = 0; k < w; k++){
                    if(dp1[i][j][k]!=-1&&dp1[i][j][k]<w&&dp1[i][j][dp1[i][j][k]]!=-1)
                    dp1[i][j][k]=dp1[i][j][dp1[i][j][k]];
                }
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = 0; j < w; j++){
                for (int k = 0; k < h; k++){
                    if(dp2[i][j][k]!=-1&&dp2[i][j][k]<h&&dp2[i][j][dp2[i][j][k]]!=-1)
                    dp2[i][j][k]=dp2[i][j][dp2[i][j][k]];
                }
            }
        }
        for (int i = 0; i < h; i++){
            for (int j = 0; j < h; j++){
                for (int k = 0; k < w; k++){
                    if(dp1[i][j][k]!=-1){
                    int l = dp1[i][j][k]-1;
                    dp2[k][l][i] = max(dp2[k][l][i], j+1);
                }
                    
                }
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = 0; j < w; j++){
                for (int k = 0; k < h; k++){
                    if(dp2[i][j][k]!=-1){
                    int l = dp2[i][j][k]-1;
                    dp1[k][l][i] = max(dp1[k][l][i], j+1);
                    }
                    
                }
            }
        }
        for (int k = w-1; k >= 0; k--){
            for (int i = 0; i < h; i++){
                for (int j = h-1; j >= i; j--){
                    if(i>0) dp1[i][j][k] = max(dp1[i][j][k], dp1[i-1][j][k]);
                    if(j+1<h) dp1[i][j][k] = max(dp1[i][j][k], dp1[i][j+1][k]);
                }
            }
        }
        for (int k = h-1; k >= 0; k--){
            for (int i = 0; i < w; i++){
                for (int j = w-1; j >= i; j--){
                    if(i>0) dp2[i][j][k] = max(dp2[i][j][k], dp2[i-1][j][k]);
                    if(j+1<w) dp2[i][j][k] = max(dp2[i][j][k], dp2[i][j+1][k]);
                }
            }
        }
        for (int i = 0; i < h; i++){
            for (int j = i; j < h; j++){
                for (int k = 1; k < w; k++){
                    dp1[i][j][k] = max(dp1[i][j][k], dp1[i][j][k-1]);
                    
                }
            }
        }
        for (int i = 0; i < w; i++){
            for (int j = i; j < w; j++){
                for (int k = 1; k < h; k++){
                    dp2[i][j][k] = max(dp2[i][j][k], dp2[i][j][k-1]);
                    
                }
            }
        }
        
    }

}

Measured memory_bytes: 62848