#include <algorithm>
#include <bitset>
#include <cassert>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<unsigned char, unsigned char> pcc;
typedef pair<double, double> pdd;
typedef vector<vector<ll>> matrix;

const int MAX_ANS = 16;
unsigned char horizontal[185][185][185][MAX_ANS + 1];
unsigned char vertical[185][185][185][MAX_ANS + 1];
string g[185];
int prefSum[186][186];
int getArea(int a, int b, int c, int d) {
  assert(a <= c && b <= d);
  // get area of (a, b) to (c, d) zero-indexed
  return prefSum[c+1][d+1] - prefSum[a][d+1] - prefSum[c+1][b] + prefSum[a][b];
}
int r, c;

void refresh(int complexity) {
  for(int i = 0; i < r; i++) {
    for(int j = i; j < r; j++) {
      for(int k = 0; k < c; k++) {
        // we want to update horizontal[i][j][k][complexity]
        int lhs = k-1;
        int rhs = c-1;
        while(lhs != rhs) {
          int mid = (lhs+rhs+1)/2;
          if(vertical[k][mid][i][complexity] >= j-i+1) lhs = mid;
          else rhs = mid-1;
        }
        if(lhs-k+1 >  horizontal[i][j][k][complexity]) {
          int orig = horizontal[i][j][k][complexity];
          horizontal[i][j][k][complexity] = lhs-k+1;
        }
      }
    }
  }
  for(int i = 0; i < c; i++) {
    for(int j = i; j < c; j++) {
      for(int k = 0; k < r; k++) {
        // we want to update vertical[i][j][k][complexity]
        int lhs = k-1;
        int rhs = r-1;
        while(lhs != rhs) {
          int mid = (lhs+rhs+1)/2;
          if(horizontal[k][mid][i][complexity] >= j-i+1) lhs = mid;
          else rhs = mid-1;
        }
        if(lhs-k+1 > vertical[i][j][k][complexity]) {
          vertical[i][j][k][complexity] = lhs-k+1;
        }
      }
    }
  }
}

void solve() {
  cin >> r >> c;
  for(int i = 0; i < r; i++) cin >> g[i];
  for(int i = 0; i < r; i++) {
    for(int j = 0; j < c; j++) {
      prefSum[i+1][j+1] = prefSum[i+1][j] + prefSum[i][j+1] - prefSum[i][j];
      if(g[i][j] == '#') prefSum[i+1][j+1]++;
    }
  }
  for(int i = 0; i < r; i++) {
    for(int j = i; j < r; j++) {
      int rhs = 0;
      for(int k = 0; k < c; k++) {
        rhs = max(rhs, k);
        while(rhs < c) {
          int area = getArea(i, k, j, rhs);
          if(area != 0 && area != (rhs-k+1) * (j-i+1)) {
            break;
          }
          rhs++;
        }
        horizontal[i][j][k][0] = rhs-k;
      }
    }
  }
  for(int i = 0; i < c; i++) {
    for(int j = i; j < c; j++) {
      int rhs = 0;
      for(int k = 0; k < r; k++) {
        rhs = max(rhs, k);
        while(rhs < r) {
          int area = getArea(k, i, rhs, j);
          if(area != 0 && area != (rhs-k+1) * (j-i+1)) {
            break;
          }
          rhs++;
        }
        vertical[i][j][k][0] = rhs-k;
      }
    }
  }
  int ret = 0;
  while(ret < MAX_ANS && (horizontal[0][r-1][0][ret] < c && vertical[0][c-1][0][ret] < r)) {
    for(int i = 0; i < r; i++) {
      for(int j = i; j < r; j++) {
        for(int k = 0; k < c; k++) {
          if(k + horizontal[i][j][k][ret] == c) {
            horizontal[i][j][k][ret+1] = horizontal[i][j][k][ret];
          }
          else {
            horizontal[i][j][k][ret+1] = horizontal[i][j][k][ret] + horizontal[i][j][k+horizontal[i][j][k][ret]][ret];
          }
          assert(k + horizontal[i][j][k][ret+1] <= c);
        }
      }
    }
    for(int i = 0; i < c; i++) {
      for(int j = i; j < c; j++) {
        for(int k = 0; k < r; k++) {
          if(k + vertical[i][j][k][ret] == r) {
            vertical[i][j][k][ret+1] = vertical[i][j][k][ret];
          }
          else {
            vertical[i][j][k][ret+1] = vertical[i][j][k][ret] + vertical[i][j][k+vertical[i][j][k][ret]][ret];
          }
          assert(k + vertical[i][j][k][ret+1] <= r);
        }
      }
    }
    refresh(++ret);
  }
  cout << ret << "\n";
}

void casesolve() {
  int t;
  cin >> t;
  for(int i = 1; i <= t; i++) {
    cout << "Case #" << i << ":" << "\n";
    solve();
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  solve();
  // casesolve();
}


Measured memory_bytes: 209280