#include <bits/stdc++.h>

const int N = 190;

short G[N][N], a[N][N][N][18], b[N][N][N][18], sum1[N][N], sum2[N][N]; /// a->  b|
char str[N];

inline short get1(short l, short r, short i) { /// |
    return sum1[r][i] - sum1[l - 1][i];
}
inline short get2(short i, short l, short r) { /// -
    return sum2[i][r] - sum2[i][l - 1];
}

inline void exmax(short &a, const short &b) {
    a < b ? a = b : 0;
    return;
}
inline short Min(const short &a, const short &b) {
    return a > b ? b : a;
}

int main() {


    //printf("%d \n", (sizeof(a) * 2 + sizeof(G) + sizeof(str)) / 1048576);
    short n, m;
    scanf("%hd%hd", &n, &m);
    for(register short i(1); i <= n; ++i) {
        scanf("%s", str + 1);
        for(register short j(1); j <= m; ++j) {
            G[i][j] = (str[j] == '#');
            sum1[i][j] = sum1[i - 1][j] + G[i][j];
            sum2[i][j] = sum2[i][j - 1] + G[i][j];
            //printf("%hd %hd G = %hd \n", i, j, G[i][j]);
        }
    }

    for(register short l(1); l <= n; ++l) {
        for(register short r(l); r <= n; ++r) {
            short last = -1;
            for(register short i(m); i >= 1; --i) {
                short tot(get1(l, r, i));
                //printf("tot = %hd \n", tot);
                if(tot != 0 && tot != r - l + 1) {
                    a[l][r][i][0] = i - 1;
                    last = -1;
                }
                else if(last != tot) {
                    a[l][r][i][0] = i;
                    last = tot;
                }
                else {
                    a[l][r][i][0] = a[l][r][i + 1][0];
                }
                //printf("a %hd %hd %hd 0 = %hd \n", l, r, i, a[l][r][i][0]);
            }
        }
    }

    /*for(short l = 1; l <= m; l++) {
        for(short r = l; r <= m; r++) {
            short last = -1;
            for(short i = n; i >= 1; i--) {
                short tot = get2(i, l, r);
                //printf("tot = %hd \n", tot);
                if(tot != r - l + 1 && tot != 0) {
                    b[i][l][r][0] = i - 1;
                    last = -1;
                }
                else if(tot != last) {
                    b[i][l][r][0] = i;
                    last = tot;
                }
                else {
                    b[i][l][r][0] = b[i + 1][l][r][0];
                }
                //printf("b %hd %hd %hd 0 = %hd \n", i, l, r, b[i][l][r][0]);
            }
        }
    }*/

    if(a[1][n][1][0] == m) {
        printf("%hd\n", 0);
        return 0;
    }

    for(register short j(1); j <= 17; ++j) {

        //printf("------------------- %hd -------------------- \n", j);
        for(register short i(m); i >= 1; --i) {
            for(register short l(1); l <= n; ++l) {
                short p = l;
                for(register short r(l); r <= n; ++r) {
                    exmax(a[l][r][i][j], a[l][r][i][j - 1]);
                    short t(a[l][r][i][j - 1]);
                    t = a[l][r][t + 1][j - 1];
                    exmax(a[l][r][i][j], t);
                    if(a[l][r][i][j] == m || l == r) continue;

                    for(; p < r; ++p) {
                        /// p [l, p] [p + 1, r]
                        t = Min(a[l][p][i][j - 1], a[p + 1][r][i][j - 1]);
                        // if(a[l][p][i][j - 1] <= a[l][r][i][j]) break;
                        if(a[l][p][i][j - 1] == i - 1) break;
                        if(p == r - 1 || Min(a[l][p + 1][i][j - 1], a[p + 2][r][i][j - 1]) < t) {
                            exmax(a[l][r][i][j], t);
                            break;
                        }
                    }

                }
            }
            if(a[1][n][1][j] == m) {
                printf("%hd\n", j);
                return 0;
            }
        }

        /*for(short l = 1; l <= n; l++) {
            for(short r = l; r <= n; r++) {
                for(short i = 1; i <= m; i++) {
                    printf("a %hd %hd %hd = %hd \n", l, r, i, a[l][r][i][j]);
                }
            }
        }
        printf("--------------- \n");
        for(short l = 1; l <= m; l++) {
            for(short r = l; r <= m; r++) {
                for(int i = 1; i <= n; i++) {
                    printf("b %hd %hd %hd = %hd \n", i, l, r, b[i][l][r][j]);
                }
            }
        }
        puts("");*/
    }

    /*for(register short i = 0; i <= 17; i++) {
        if(a[1][n][1][i] == m) {
            printf("%hd\n", i);
            break;
        }
        if(b[1][1][m][i] == n) {
            printf("%hd\n", i);
            break;
        }
    }*/

    return 0;
}


Measured memory_bytes: 238080