#include <bits/stdc++.h>
#define pb push_back
#define pll pair <ll, ll>
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair <pair <ll, ll>, ll>
#define pllll pair <pair <ll, ll>, pair <ll, ll> >
#define psl pair <string, ll>
#define pld pair <ld, ld>
#define all(a) a.begin(), a.end()
#define vvl vector <vector <ll> >
#define cld complex <double>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 190;
const ll maxans = 20;
const ll inf = 1e9;

ll n, m, k, t;

// ll D[maxn][maxn][maxn][maxans];

string S[maxn];

int main() {
    pyshnapyshnakaa
    ll q, w, e, a, b, c;
    vector <vector <vector <ll> > > D;
    cin >> n >> m;
    D.resize(n);
    for (q = 0; q < D.size(); q++) {
        D[q].resize(n);
        for (w = 0; w < D[q].size(); w++) {
            D[q][w].resize(m, -1);
        }
    }
    vector <vector <vector <ll> > > D0 = D;
    for (q = 0; q < n; q++) {
        cin >> S[q];
    }
    for (q = 0; q < n; q++) {
        vector <ll> A(m);
        for (w = q; w < n; w++) {
            for (e = 0; e < m; e++) {
                if (S[w][e] == '.') {
                    A[e] |= 1;
                }
                else {
                    A[e] |= 2;
                }
            }
            ll start = 0, cur = 0;
            for (e = 0; e < m; e++) {
                // cout << e << " " << cur << " E CUR" << endl;
                cur |= A[e];
                if (cur == 3) {
                    for (ll i = start; i < e; i++) {
                        D[q][w][i] = e - 1;
                    }
                    cur = A[e];
                    start = e;
                    if (cur == 3) {
                        D[q][w][e] = e - 1;
                        start++;
                        cur = 0;
                    }
                }
            }
            for (ll i = start; i < m; i++) {
                // cout << "I " << i << " " << q << " " << w << endl;
                // cout << D.size() << endl;
                D[q][w][i] = m - 1;
            }
    // cout << "FALL" << endl;
        }
    }
    for (ll it = 0; it < maxans; it++) {
        if (D[0][n - 1][0] == m - 1) {
            cout << it << endl;
            return 0;
        }
        vector <vector <vector <ll> > > D1 = D;
        // D = D0;
        for (q = 0; q < n; q++) {
            for (w = q; w < n; w++) {
                for (e = 0; e < m; e++) {
                    ll i1 = D[q][w][e];
                    if (i1 != m - 1) {
                        D[q][w][e] = max(D[q][w][e], D[q][w][i1 + 1]);
                    }
                }
            }
        }
        // cout << "ITER " << it << endl;
        // for (q = 0; q < D.size(); q++) {
        //     for (w = q; w < D[q].size(); w++) {
        //         for (e = 0; e < D[q][w].size(); e++) {
        //             cout << q << " " << w << " " << e << " D " << D[q][w][e] << endl;
        //         }
        //         cout << endl;
        //     }
        //     cout << endl;
        // }
        // cout << endl << endl;
        // cout << D1[1][2][0] << endl;
        for (q = 0; q < n; q++) {
            for (e = 0; e < m; e++) {
                ll besti = q;
                for (w = q; w < n; w++) {
                    ll dd = -1;
                    for (ll i = besti; i < w; i++) {
                        ll ddd = min(D1[q][i][e], D1[i + 1][w][e]);
                        // if (e == m - 1 && q == 0 && w == n - 1 && ddd == m - 1) {
                        //     // cout << i << " LIAR! I " << D1[q][i][e] << "  << endl;
                        // } 
                        if (ddd >= dd) {
                            dd = ddd;
                            besti = i;
                        }
                        else {
                            break;
                        }
                    }
                    D[q][w][e] = max(D[q][w][e], dd);
                    // cout << q << " " << w << " " << e << D[q][w][e]
                }
            }
        }
    }
    cout << "END" << endl;
    return 0;
}

Measured memory_bytes: 154976