#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <ctime>
#include <cassert>
#include <unordered_map>
#include <fstream>
#include <random>
#include <cstring>
#include <bitset>
#include <functional>
#include <tuple>
#include <complex>
#include <chrono>
#include <climits>

#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef unsigned int uint;
typedef unsigned long long ull;

mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
// default_random_engine generator;
// mt19937 rnd(1);

const int MAXN = 186;
const int MAXL = 18;

int dp[MAXN][MAXL][MAXN][MAXN];

int f[MAXN][MAXN];
int g[MAXN][MAXN];

int u[MAXN][MAXN];
string s[MAXN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < n; ++i) {
        cin >> s[i];
    }

    for (int i = 1; i < n; ++i) {
        for (int j = 0; j < m; ++j) {
            if (s[i][j] == s[i - 1][j]) {
                u[i][j] = u[i - 1][j];
            } else {
                u[i][j] = i;
            }
        }
    }

    // for (int i = 0; i < n; ++i) {
    //     for (int j = 0; j < m; ++j) {
    //         cout << u[i][j];
    //     }
    //     cout << endl;
    // }

    for (int i = 0; i < n; ++i) {
        for (int l = 0; l < MAXL; ++l) {
            for (int j = 0; j < m; ++j) {
                for (int k = j + 1; k <= m; ++k) {
                    dp[i][l][j][k] = 2 * n;
                }
            }
        }
        // cout << i << endl;

        for (int j = 0; j < m; ++j) {
            int cur = 0;
            for (int k = j + 1; j <= m; ++k) {
                if (s[i][j] != s[i][k - 1]) {
                    break;
                }
                cur = max(cur, u[i][k - 1]);
                dp[i][0][j][k] = cur;
            }
        }

        for (int l = 1; l < MAXL; ++l) {
            for (int j = 0; j < m; ++j) {
                for (int k = j + 1; k <= m; ++k) {
                    int d = dp[i][l - 1][j][k];

                    // cout << i << " " << j << " " << k << " " << l << endl;
                    // if (i == 2 && j == 0 && k == 2) {
                    //     cout << l << " " << d << endl;
                    // }
                    if (d > i) {
                        continue;
                    }
                    dp[i][l][j][k] = min(dp[i][l][j][k], d);
                    if (d > 0 && dp[d - 1][l - 1][j][k] <= d - 1) {
                        dp[i][l][j][k] = min(dp[i][l][j][k], dp[d - 1][l - 1][j][k]);
                    }
                }
            }   


            for (int k = 0; k <= i; ++k) {
                int pos = m;    
                for (int j = m - 1; j >= 0; --j) {
                    while (pos > j && dp[i][l - 1][j][pos] > k) {
                        --pos;
                    }
                    f[k][j] = pos;
                }

                pos = 0;

                for (int j = 1; j <= m; ++j) {
                    int pos = 0;
                    while (pos < j && dp[i][l - 1][pos][j] > k) {
                        ++pos;
                    }
                    g[k][j] = pos;
                }
            }

            for (int j = 0; j < m; ++j) {
                int h = 0;
                for (int k = j + 1; k <= m; ++k) {
                    while (h <= i && f[h][j] < g[h][k]) {
                        ++h;
                    }

                    if (h == i + 1) {
                        break;
                    }

                    dp[i][l][j][k] = min(dp[i][l][j][k], h);
                }
            }
        }
    }

    // for (int i = 0; i < n; ++i) {
    //     for (int l = 1; l < 5; ++l) {
    //         for (int j = 0; j < m; ++j) {
    //             for (int k = j + 1; k <= m; ++k) {
    //                 cout << i << " " << l << " " << j << " " << k << " " << dp[i][l][j][k] << endl;
    //             }
    //         }
    //     }
    // }
    

    int res = 0;
    while (dp[n - 1][res][0][m] != 0) {
        ++res;
    }

    cout << res << endl;
}


Measured memory_bytes: 450816