#include <bits/stdc++.h>

using namespace std;

int h,w;
int r[2][186][186][186];
int d[2][186][186][186];
bool f[185][185];

int solve(){
    int i,j,k,x,y,z;
    string str;
    cin >> h >> w;
    for(i = 0;i < h;i++){
        cin >> str;
        for(j = 0;j < w;j++){
            f[i][j] = (str[j] == '.');
        }
    }
    int ans;
    memset(d,-1,sizeof(d));
    memset(r,-1,sizeof(r));
    for(i = 0;i < h;i++){
        for(j = 0;j < w;j++){
            k = w;
            for(y = i;y < h;y++){
                for(x = j;x < k;x++){
                    if(f[i][j] == f[y][x]){
                        d[0][i][j][x+1] = max(d[0][i][j][x+1],y+1);
                        r[0][j][i][y+1] = max(r[0][j][i][y+1],x+1);
                    }
                    else{
                        k = x;
                        break;
                    }
                }
            }
        }
    }
    int n,o;
    for(ans = 0;;ans++){
        o = ans%2;
        n = !o;
        if(d[o][0][0][w] == h) break;
        for(i = 0;i < h;i++){
            for(j = 0;j < w;j++){
                for(x = w;x > j;x--){
                    d[n][i][j][x] = d[o][i][j][x];
                }
            }
        }
        for(j = 0;j < w;j++){
            for(i = 0;i < h;i++){
                for(y = h;y > i;y--){
                    r[n][j][i][y] = r[o][j][i][y];
                }
            }
        }
        for(i = 0;i < h;i++){
            for(j = 0;j < w;j++){
                for(x = j+1;x <= w;x++){
                    y = d[o][i][j][x];
                    if(y == -1 || y == h) continue;
                    z = d[o][y][j][x];
                    if(z != -1){
                        d[n][i][j][x] = max(d[n][i][j][x],z);
                        r[n][j][i][z] = max(r[n][j][i][z],x);
                    }
                }
            }
        }
        for(j = 0;j < w;j++){
            for(i = 0;i < h;i++){
                for(y = i+1;y <= h;y++){
                    x = r[o][j][i][y];
                    if(x == -1 || x == w) continue;
                    z = r[o][x][i][y];
                    if(z != -1){
                        r[n][j][i][y] = max(r[n][j][i][y],z);
                        d[n][i][j][z] = max(d[n][i][j][z],y);
                    }
                }
            }
        }
        for(i = 0;i < h;i++){
            for(j = 0;j < w;j++){
                for(x = w;x > j;x--){
                    if(i > 0 && d[n][i-1][j][x] > i) d[n][i][j][x] = max(d[n][i][j][x],d[n][i-1][j][x]);
                    if(j > 0) d[n][i][j][x] = max(d[n][i][j][x],d[n][i][j-1][x]);
                    if(x < w) d[n][i][j][x] = max(d[n][i][j][x],d[n][i][j][x+1]);
                }
            }
        }
        for(j = 0;j < w;j++){
            for(i = 0;i < h;i++){
                for(y = h;y > i;y--){
                    if(j > 0 && r[n][j-1][i][y] > j) r[n][j][i][y] = max(r[n][j][i][y],r[n][j-1][i][y]);
                    if(i > 0) r[n][j][i][y] = max(r[n][j][i][y],r[n][j][i-1][y]);
                    if(y < h) r[n][j][i][y] = max(r[n][j][i][y],r[n][j][i][y+1]);
                }
            }
        }
    }
    return ans;
}

int main(){
    cout << solve() << endl;
}


Measured memory_bytes: 100864