#include <cstdio>
#include <algorithm>
#include <vector>
#include <set>
#include <cstdlib>
#include <utility>
#include <cmath>
#include <queue>
#include <stack>
#include <cstring>

using namespace std;

#define ll long long

#ifndef ONLINE_JUDGE
#define debug(format, ...) fprintf(stderr, \
    "%s:%d: " format "\n", __func__, __LINE__,##__VA_ARGS__)
#else
#define debug(format, ...)
#define NDEBUG
#endif

/* top row, bottom row, left col */
int hor[200][200][200];
/* left col, right col, top row */
int ver[200][200][200];

int tmp[200][200][200];
int psum[200][200];

char grid[200][200];
char s[200];

int n, m;

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
        scanf("%s", s + 1);
        for (int j = 1; j <= m; j++) {
            grid[i][j] = (s[j] == '#');
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= m; j++) {
            psum[i][j] = psum[i-1][j] + psum[i][j-1] - psum[i-1][j-1] + (grid[i][j] ? 1 : 0);
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            for (int k = 1; k <= m; k++) {
                int l = k - 1; int r = m;
                while (l < r) {
                    int mid = (l + r) / 2 + 1;
                    int sum_val = psum[j][mid] - psum[i-1][mid] - psum[j][k-1] + psum[i-1][k-1];
                    if (sum_val == 0 || sum_val == (j - i + 1) * (mid - k + 1)) {
                        l = mid;
                    } else {
                        r = mid - 1;
                    }
                }
                hor[i][j][k] = l;
            }
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = i; j <= m; j++) {
            for (int k = 1; k <= n; k++) {
                int l = k - 1; int r = n;
                while (l < r) {
                    int mid = (l + r) / 2 + 1;
                    int sum_val = psum[mid][j] - psum[mid][i-1] - psum[k-1][j]
                        + psum[k-1][i-1];
                    if (sum_val == 0 || sum_val == (j - i + 1) * (mid - k + 1)) {
                        l = mid;
                    } else {
                        r = mid - 1;
                    }
                }
                ver[i][j][k] = l;
            }
        }
    }
    if (hor[1][n][1] == m) {
        printf("0"); return 0;
    }
    for (int complexity = 1; ; complexity++) {
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                for (int k = 1; k <= m; k++) {
                    if (hor[i][j][k] < m)
                        hor[i][j][k] = hor[i][j][hor[i][j][k] + 1];
                }
            }
        }
        /*if (complexity <= 2) {
            for (int i = 1; i <= n; i++) {
                for (int j = i; j <= n; j++) {
                    for (int k = 1; k <= m; k++) {
                        printf("hor[%d][%d][%d] = %d\n", i, j, k, hor[i][j][k]);
                    }
                }
            }
        }*/
        for (int i = 1; i <= m; i++) {
            for (int j = i; j <= m; j++) {
                for (int k = 1; k <= n; k++) {
                    if (ver[i][j][k] < n)
                        ver[i][j][k] = ver[i][j][ver[i][j][k] + 1];
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                for (int k = 1; k <= m; k++) {
                    int l = k - 1; int r = m;
                    while (l < r) {
                        int mid = (l + r) / 2 + 1;
                        if (ver[k][mid][i] >= j) {
                            l = mid;
                        } else {
                            r = mid - 1;
                        }
                    }
                    tmp[i][j][k] = max(hor[i][j][k], l);
                }
            }
        }
        for (int i = 1; i <= m; i++) {
            for (int j = i; j <= m; j++) {
                for (int k = 1; k <= n; k++) {
                    int l = k - 1; int r = n;
                    while (l < r) {
                        int mid = (l + r) / 2 + 1;
                        if (hor[k][mid][i] >= j) {
                            l = mid;
                        } else {
                            r = mid - 1;
                        }
                    }
                    ver[i][j][k] = max(ver[i][j][k], l);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                for (int k = 1; k <= m; k++) {
                    hor[i][j][k] = tmp[i][j][k];
                }
            }
        }
        if (hor[1][n][1] == m) {
            printf("%d", complexity); return 0;
        }
    }
    return 0;
}


Measured memory_bytes: 45932