#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long



#if 0
const int MOD = ;

inline int lgput(int a, int b) {
    int ans = 1;
    while(b > 0) {
        if(b & 1) ans = (1LL * ans * a) % MOD;
        b >>= 1;
        a = (1LL * a * a) % MOD;
    }
    return ans;
}

inline void mod(int &x) {
    if(x >= MOD)
        x -= MOD;
}

inline void add(int &x, int y) {
    x += y;
    mod(x);
}

inline void sub(int &x, int y) {
    x += MOD - y;
    mod(x);
}

inline void mul(int &x, int y) {
    x = (1LL * x * y) % MOD;
}

inline int inv(int x) {
    return lgput(x, MOD - 2);
}
#endif

#if 0
int fact[], invfact[];

inline void prec(int n) {
    fact[0] = 1;
    for(int i = 1; i <= n; i++) {
        fact[i] = (1LL * fact[i - 1] * i) % MOD;
    }
    invfact[n] = lgput(fact[n], MOD - 2);
    for(int i = n - 1; i >= 0; i--) {
        invfact[i] = (1LL * invfact[i + 1] * (i + 1)) % MOD;
    }
}

inline int comb(int n, int k) {
    if(n < k) return 0;
    return (1LL * fact[n] * (1LL * invfact[k] * invfact[n - k] % MOD)) % MOD;
}
#endif

using namespace std;

const int MAXN = 185;

char mat[MAXN + 1][MAXN + 1];
int sp[MAXN + 1][MAXN + 1];

int dpl[2][MAXN + 1][MAXN + 1][MAXN + 1];
int dpc[2][MAXN + 1][MAXN + 1][MAXN + 1];

int main() {
#if 0
    ifstream cin("A.in");
    ofstream cout("A.out");
#endif
    int i, j, n, m;
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    cin >> n >> m;
    for(i = 1; i <= n; i++) {
        cin >> mat[i] + 1;
        for(j = 1; j <= m; j++) {
            sp[i][j] = sp[i - 1][j] + sp[i][j - 1] - sp[i - 1][j - 1] + (mat[i][j] == '.');
        }
    }

    auto get = [&](int l1, int c1, int l2, int c2) {
        return sp[l2][c2] - sp[l1 - 1][c2] - sp[l2][c1 - 1] + sp[l1 - 1][c1 - 1];
    };

    for(int l1 = 1; l1 <= n; l1++) {
        for(int c1 = 1; c1 <= m; c1++) {
            for(int l2 = l1; l2 <= n; l2++) {
                int res = c1 - 1;
                for(int step = 1 << 7; step; step >>= 1) {
                    if(res + step <= m) {
                        int cur = get(l1, c1, l2, res + step);
                        if(cur == 0 || cur == (l2 - l1 + 1) * (res + step - c1 + 1)) {
                            res += step;
                        }
                    }
                }
                dpl[0][l1][c1][l2] = res + 1;
            }
            for(int c2 = c1; c2 <= m; c2++) {
                int res = l1 - 1;
                for(int step = 1 << 7; step; step >>= 1) {
                    if(res + step <= n) {
                        int cur = get(l1, c1, res + step, c2);
                        if(cur == 0 || cur == (res + step - l1 + 1) * (c2 - c1 + 1)) {
                            res += step;
                        }
                    }
                }
                dpc[0][l1][c1][c2] = res + 1;
            }
        }
    }

    if(dpl[0][1][1][n] == m + 1) {
        cout << 0;
        return 0;
    }

    for(int o = 1; o < 17; o++) {
        for(int l1 = 1; l1 <= n; l1++) {
            for(int c1 = 1; c1 <= m; c1++) {
                for(int l2 = l1; l2 <= n; l2++) {
                    int cur = dpl[1 - o & 1][l1][c1][l2];
                    if(cur == m + 1) {
                        dpl[o & 1][l1][c1][l2] = m + 1;
                    }
                    else {
                        dpl[o & 1][l1][c1][l2] = dpl[1 - o & 1][l1][cur][l2];
                    }
                }
                for(int c2 = c1; c2 <= m; c2++) {
                    int cur = dpc[1 - o & 1][l1][c1][c2];
                    if(cur == n + 1) {
                        dpc[o & 1][l1][c1][c2] = n + 1;
                    }
                    else {
                        dpc[o & 1][l1][c1][c2] = dpc[1 - o & 1][cur][c1][c2];
                    }
                }
            }
        }


        for(int l1 = 1; l1 <= n; l1++) {
            for(int c1 = 1; c1 <= m; c1++) {
                for(int l2 = l1; l2 <= n; l2++) {
                    int c2 = c1;
                    while(c2 < dpl[o & 1][l1][c1][l2]) {
                        dpc[o & 1][l1][c1][c2] = max(dpc[o & 1][l1][c1][c2], l2 + 1);
                        c2++;
                    }
                }
                for(int c2 = c1; c2 <= m; c2++) {
                    int l2 = l1;
                    while(l2 < dpc[o & 1][l1][c1][c2]) {
                        dpl[o & 1][l1][c1][l2] = max(dpl[o & 1][l1][c1][l2], c2 + 1);
                        l2++;
                    }
                }
            }
        }

        //cerr << dpl[o & 1][1][1][n] << "\n";
        if(dpl[o & 1][1][1][n] == m + 1 || dpc[o & 1][1][1][m] == n + 1) {
            cout << o;
            return 0;
        }
    }

    return 0;
}



Measured memory_bytes: 100992