#include <bits/stdc++.h>
using namespace std;

const int M = 190, N = 190, LG = 17;

int m, n, sum[M][N];
int row[M][M][N][2], col[N][N][M][2], sr[M][M][N], sc[N][N][M];
char s[M][N];

bool valid(int l, int r, int u, int d) {
    int cur = sum[d][r] - sum[d][l - 1] - sum[u - 1][r] + sum[u - 1][l - 1];
    return cur == 0 || cur == (r - l + 1) * (d - u + 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> m >> n;
    for (int i = 1; i <= m; i++) {
        cin >> (s[i] + 1);
        for (int j = 1; j <= n; j++) {
            sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + (s[i][j] == '.');
        }
    }
    for (int i = 1; i <= m; i++) {
        for (int j = i; j <= m; j++) {
            for (int k = 1; k <= n; k++) {
                row[i][j][k][0] = n + 1;
                for (int l = k; l <= n; l++) {
                    if (!valid(k, l, i, j)) {
                        row[i][j][k][0] = l;
                        break;
                    }
                }
            }
        }
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i; j <= n; j++) {
            for (int k = 1; k <= m; k++) {
                col[i][j][k][0] = m + 1;
                for (int l = k; l <= m; l++) {
                    if (!valid(i, j, k, l)) {
                        col[i][j][k][0] = l;
                        break;
                    }
                }
            }
        }
    }
    for (int co = 1; co < LG; co++) {
        int now = co & 1, prv = now ^ 1;
        for (int i = 1; i <= m; i++) {
            for (int j = i; j <= m; j++) {
                for (int k = 1; k <= n; k++) {
                    row[i][j][k][now] = row[i][j][k][prv];
                    if (row[i][j][k][now] == n + 1) {
                        continue;
                    }
                    sr[i][j][k] = co;
                    row[i][j][k][now] = row[i][j][row[i][j][k][prv]][prv];
                    for (int l = row[i][j][k][now]; l <= n + 1; l++) {
                        if (l == n + 1) {
                            row[i][j][k][now] = n + 1;
                            break;
                        }
                        int cur = col[k][l][i][prv];
                        if (cur != m + 1) {
                            cur = col[k][l][cur][prv];
                        }
                        if (cur <= j) {
                            row[i][j][k][now] = l;
                            break;
                        }
                    }
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = i; j <= n; j++) {
                for (int k = 1; k <= m; k++) {
                    col[i][j][k][now] = col[i][j][k][prv];
                    sc[i][j][k] = co;
                    if (col[i][j][k][now] == m + 1) {
                        continue;
                    }
                    col[i][j][k][now] = col[i][j][col[i][j][k][prv]][prv];
                    for (int l = col[i][j][k][now]; l <= m + 1; l++) {
                        if (l == m + 1) {
                            col[i][j][k][now] = m + 1;
                            break;
                        }
                        int cur = row[k][l][i][prv];
                        if (cur != n + 1) {
                            cur = row[k][l][cur][prv];
                        }
                        if (cur <= j) {
                            col[i][j][k][now] = l;
                            break;
                        }
                    }
                }
            }
        }
    }
    cout << sr[1][m][1];
}


Measured memory_bytes: 160000