#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1<<30)-1)
#define LINF (1LL<<60)
#define EPS (1e-10)
typedef short Int;
typedef pair<Int, Int> P;

Int ldp[185][185][185][20];
Int udp[185][185][185][20];
int h, w;
string field[185];
int cnt[185][185];

int cnt_white(Int x1, Int y1, Int x2, Int y2){
    int ans = 0;
    if(x1 > 0 && y1 > 0)ans += cnt[x1-1][y1-1];
    if(x1 > 0)ans -= cnt[x1-1][y2];
    if(y1 > 0)ans -= cnt[x2][y1-1];
    ans += cnt[x2][y2];
    return ans;
}

int main(){
    cin >> h >> w;
    for(Int i = 0;i < h;i++){
        cin >> field[i];
        for(Int j = 0;j < w;j++){
            if(field[i][j] == '#')cnt[i][j]++;
        }
    }
    for(Int i = 1;i < h;i++)
        for(Int j = 0;j < w;j++)
            cnt[i][j] += cnt[i-1][j];
    
    for(Int i = 0;i < h;i++)
        for(Int j = 1;j < w;j++)
            cnt[i][j] += cnt[i][j-1];

    for(Int c = 0;c < 20;c++){
        for(Int i = 0;i < h;i++){
            for(Int j = i;j < h;j++){
                for(Int k = w-1;k >= 0;k--){
//                    cout << (int)c<< " " << (int)i << " " << (int)j << " " << (int)k << endl;
                    if(c == 0){
                        Int &r = ldp[i][j][k][c];
                        if(cnt_white(i, k, j, k) == 0 || cnt_white(i, k, j, k) == j - i + 1){                        
                            if(k == w-1 || ldp[i][j][k+1][c] == -1 || field[i][k] != field[i][k+1])r = k+1;
                            else r = ldp[i][j][k+1][c];
                        }
                        else r = -1;
                    }
                    else{
                        Int &r = ldp[i][j][k][c];
                        Int nxt = ldp[i][j][k][c-1];
                        if(nxt == -1)r = -1;
                        else if(nxt == w || ldp[i][j][nxt][c-1] == -1)r = nxt;
                        else r = ldp[i][j][nxt][c-1];
                    }
                }
            }
        }
        for(Int i = 0;i < w;i++){
            for(Int j = i;j < w;j++){
                for(Int k = h-1;k >= 0;k--){
                    if(c == 0){
                        Int &r = udp[i][j][k][c];
                        if(cnt_white(k, i, k, j) == 0 || cnt_white(k, i, k, j) == j - i + 1){                        
                            if(k == h-1 || udp[i][j][k+1][c] == -1 || field[k][i] != field[k+1][j])r = k+1;
                            else r = udp[i][j][k+1][c];
                        }
                        else r = -1;
                    }
                    else{
                        Int &r = udp[i][j][k][c];
                        Int nxt = udp[i][j][k][c-1];
                        if(nxt == -1)r = -1;
                        else if(nxt == h || udp[i][j][nxt][c-1] == -1)r = nxt;
                        else r = udp[i][j][nxt][c-1];
                    }
                }
            }
        }
        for(Int i = 0;i < h;i++){
            for(Int j = 0;j < w;j++){
                Int l = j;
                for(Int k = h-1;k >= 0;k--){
                    if(ldp[i][k][j][c] == -1)continue;
                    while(l < ldp[i][k][j][c]){
                        udp[j][l][i][c] = max(udp[j][l][i][c], (Int)(k+1));
                        l++;
                    }
                }
                Int k = i;
                for(Int l = w-1;l >= 0;l--){
                    if(udp[j][l][i][c] == -1)continue;
                    while(k < udp[j][l][i][c]){
                        ldp[i][k][j][c] = max(ldp[i][k][j][c], (Int)(l+1));
                        k++;
                    }
                }
            }
        }
/*        for(Int i = 0;i < h;i++){
            for(Int j = i;j < h;j++){
                for(Int k = 0;k < w;k++){
                    if(c < 3)
                    cout << c << " " << i << " " << j << " " << k << " " << ldp[i][j][k][c] << endl;
                }
            }
        }*/
    }
    int c = 0;
    while(c < 20 && ldp[0][h-1][0][c] < w)c++;
    cout << c << endl;
    return 0;
}

Measured memory_bytes: 493952