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

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()

int h, w;
const int maxn = 190;
string a[maxn];
int f[maxn][maxn][maxn][20];

int main(){
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> h >> w;
    for(int i = 0;i < h;i++){
        cin >> a[i];
        for(int j = w - 1;j >= 0;j--){
            if(j == w - 1){
                f[i][i][j][0] = 1;
            }else{
                if(a[i][j] == a[i][j + 1]){
                    f[i][i][j][0] = f[i][i][j + 1][0] + 1;
                }else{
                    f[i][i][j][0] = 1;
                }
            }
        }
    }
    for(int j = 0;j < w;j++){
        for(int l = 0;l < h;l++){
            for(int r = l + 1;r < h;r++){
                if(a[l][j] == a[r][j]){
                    f[l][r][j][0] = min(f[l][r - 1][j][0], f[r][r][j][0]); 
                }else{
                    f[l][r][j][0] = 0;
                }
            }
        }
    }
    for(int S = 1;S <= 16;S++){
        for(int j = 0;j < w;j++){
            for(int l = 0;l < h;l++){
                for(int r = l, m = l;r < h;r++){
                    for(;m < r and min(f[l][m][j][S - 1], f[m+1][r][j][S - 1]) <= min(f[l][m+1][j][S - 1], f[m+2][r][j][S - 1]);m++) ;
                    f[l][r][j][S] = f[l][r][j][S - 1];
                    int len = f[l][r][j][S - 1];
                    f[l][r][j][S] = max(f[l][r][j][S], len + f[l][r][j + len][S - 1]);
                    f[l][r][j][S] = max(f[l][r][j][S], min(f[l][m][j][S - 1], f[m+1][r][j][S - 1]));    
                }
            }
        }
    }
    int ans = 0;
    for(int i = 0;i <= 16;i++){
        if(f[0][h - 1][0][i] == w){
            cout << i << endl;
            return 0;
        }
    }
    return 0;
}
