#include <bits/stdc++.h>

using namespace std;
#define _for(i,j,N) for(int i = (j); i < (N);i++)
#define _rep(i,j,N) for(int i = (j); i <= (N);i++)
#define ALL(x) x.begin(),x.end()

const int maxh = 187;

int dp[2][maxh][maxh][maxh];
int lenx[maxh][maxh];
int leny[maxh][maxh];
char G[maxh][maxh];
int m,n;


int main()
{
    /*
    freopen("my.txt","w",stdout);
    printf("100 100\n");
    int ok = 0;
    _for(i,0,100){
        _for(j,0,100){
            if(ok) printf(".");
            else printf("#");
            ok = !ok;
        }
        printf("\n");
    }
    return 0;
    */
    memset(dp,-1,sizeof(dp));
    scanf("%d %d",&m,&n);
    _for(i,0,m) scanf("%s",G[i]);

    _for(x,0,m){
        for(int y = n-1;y >= 0;y--){
            if(y == n-1) leny[x][y] = n-1;
            else{
                if(G[x][y] == G[x][y+1]) leny[x][y] = leny[x][y+1];
                else leny[x][y] = y;
            }
        }
    }

    _for(y,0,n){
        for(int x = m-1;x >= 0;x--){
            if(x == m-1) lenx[x][y] = m-1;
            else{
                if(G[x][y] == G[x+1][y]) lenx[x][y] = lenx[x+1][y];
                else lenx[x][y] = x;
            }

        }
    }


    for(int y = n-1;y >= 0;y--){
        _for(d,0,m){
            _for(l,0,m){
                int r = d + l;
                if(r >= m) break;
                if(lenx[l][y] < r) {dp[0][l][r][y] = -1;continue;}
                else if(l == r) dp[0][l][r][y] = leny[l][y];
                else{
                    int s = 10000000;
                    s = min(dp[0][l][r-1][y],dp[0][l+1][r][y]);
                    dp[0][l][r][y] = s;
                }
            }
        }
    }


    int t = 0;
    int ans = 0;
    while(dp[t][0][m-1][0] != n-1){
        //printf("%d\n",dp[t][0][m-1][0]);
        t = t^1;
        ans++;
        for(int y = n-1;y >= 0;y--){
            _for(l,0,m){
                int k = l;
                _for(r,l,m){
                    int before = t^1;
                    int as = dp[before][l][r][y];
                    int big = as;
                    if(big == n-1) {dp[t][l][r][y] = big;continue;}
                    if(as >= 0 && as < n) big = max(as,dp[before][l][r][as+1]);

                    int a = dp[before][l][k][y];
                    int b = dp[before][k+1][r][y];
                    while(k+1 < r && dp[before][l][k+1][y] != -1){
                        int aa = dp[before][l][k+1][y];
                        int bb = dp[before][k+2][r][y];
                        if(min(aa,bb) >= min(a,b)){
                            k++;
                            a = aa;
                            b = bb;
                        }else break;

                    }

                    /*
                    _for(k,l,r){
                        int a = dp[before][l][k][y];
                        int b = dp[before][k+1][r][y];
                        if(a!= -1 && b !=-1)
                            big = max(big,min(a,b));
                    }
                    */
                    if(a!= -1 && b !=-1)
                            big = max(big,min(a,b));
                    //printf("y:%d l:%d r:%d k:%d val:%d\n",y,l,r,k,big);

                    dp[t][l][r][y] = big;
                }
            }
        }
    }
    /*
    int a,b,c;
    while(scanf("%d %d %d",&a,&b,&c) == 3){
        printf("%d\n",dp[1][a][b][c]);

    }
    */

    printf("%d\n",ans);

    return 0;
}


Measured memory_bytes: 51712