#include <bits/stdc++.h>
using namespace std;
#define int long long
#define REP(i, n) for (long long i = 0, max_i = (n); i < max_i; i++)
#define REPI(i, a, b) for (long long i = (a), max_i = (b); i < max_i; i++)
#define ALL(obj) begin(obj), end(obj)
#define RALL(obj) rbegin(obj), rend(obj)
#define fi first
#define se second
using ii = pair<int, int>;
vector<ii> dirs = {
  {1, 0}, {0, 1}, {-1, 0}, {0, -1},  // 4方向
  {1, 1}, {-1, 1}, {-1, -1}, {1, -1},  // 斜め
  {0, 0},  // 自身
};
template <class T> inline bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; }
template <class T> inline bool chmin(T& a, const T& b) { if (a > b) { a = b; return true; } return false; }
template <class T, class S> vector<T> make_vec(size_t n, S x) { return vector<T>(n, x); }
template <class T, class... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec<T>(ts...))>(n, make_vec<T>(ts...)); }

// debug
template <class T> ostream& operator<<(ostream& s, vector<T>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : " "); return s; }
template <class T> ostream& operator<<(ostream& s, vector<vector<T>>& d) { REP (i, d.size()) s << d[i] << (i == d.size() - 1 ? "" : "\n"); return s; }
template <class T, class S> ostream& operator<<(ostream& s, pair<T, S>& p) { s << "{" << p.first << ", " << p.second << "}"; return s; }
template <class T, class S> ostream& operator<<(ostream& s, map<T, S> m) { for (auto it = m.begin(); it != m.end(); it++) { s << *it << (next(it) == m.end() ? "" : "\n"); } return s; }
#ifdef _MY_DEBUG
  #define dump(...) cerr << "/* " << #__VA_ARGS__ << " :[" << __LINE__ << ":" << __FUNCTION__ << "]" << endl, dump_func(__VA_ARGS__), cerr << "*/\n\n";
#else
  #define dump(...)
  #define endl "\n"
#endif
void dump_func() { cerr << endl; }
template <class Head, class... Tail> void dump_func(Head&& h, Tail&&... t) { cerr << h << (sizeof...(Tail) == 0 ? "" : ", "), dump_func(forward<Tail>(t)...); }

struct Fast { Fast() { cin.tie(0); ios::sync_with_stdio(false); } } fast;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
constexpr int MOD = 1000000007;
// *************** TEMPLATE END *************** 

signed main() {
  int H, W; cin >> H >> W;
  auto acc = make_vec<int>(H + 1, W + 1, 0);
  REP (i, H) {
    REP (j, W) {
      char c;  cin >> c;
      acc[i + 1][j + 1] = acc[i + 1][j] + acc[i][j + 1] + (c == '#') - acc[i][j];
    }
  }
  auto get = [&](int i1, int i2, int j1, int j2) {
    return acc[i2 + 1][j2 + 1] - acc[i1][j2 + 1] - acc[i2 + 1][j1] + acc[i1][j1];
  };
  auto is_all = [&](int i1, int i2, int j1, int j2) {
    int tmp = get(i1, i2, j1, j2);
    return tmp == 0 || tmp == (i2 - i1 + 1) * (j2 - j1 + 1);
  };

  // dp1[k][i1][i2][j1]: 縦幅を(i1, i2)に固定、横をj1から美しさkでどこまで伸ばせるか
  auto dp1 = make_vec<int>(2, H, H, W + 1, 0);
  // dp2[k][j1][j2][i1]: 横幅を(j1, j2)に固定、縦をj1から美しさkでどこまで伸ばせるか
  auto dp2 = make_vec<int>(2, W, W, H + 1, 0);
  // 初期化
  REP (i1, H) {
    REPI (i2, i1, H) {
      dp1[0][i1][i2][W] = dp1[1][i1][i2][W] = W;
      int j2 = 0;
      REP (j1, W) {
        chmax(j2, j1);
        while (j2 < W && is_all(i1, i2, j1, j2)) j2++;
        dp1[0][i1][i2][j1] = j2;
      }
    }
  }
  REP (j1, W) {
    REPI (j2, j1, W) {
      dp2[0][j1][j2][H] = dp2[1][j1][j2][H] = H;
      int i2 = 0;
      REP (i1, H) {
        chmax(i2, i1);
        while (i2 < H && is_all(i1, i2, j1, j2)) i2++;
        dp2[0][j1][j2][i1] = i2;
      }
    }
  }
  // 更新
  for (int k = 0;; k++) {
    if (dp1[0][0][H - 1][0] == W || dp2[0][0][W - 1][0] == H) {
      cout << k << endl; return 0;
    }
    REP (i1, H) {
      REPI (i2, i1, H) {
        int j2 = 0;
        REP (j1, W) {
          chmax(j2, j1);
          while (j2 < W) {
            int md = dp2[0][j1][j2][i1];
            int en = dp2[0][j1][j2][md];
            if (en <= i2) break;
            j2++;
          }
          dp1[1][i1][i2][j1] = max(dp1[0][i1][i2][j1], j2);
        }
        REP (j1, W) {
          int md = dp1[0][i1][i2][j1];
          chmax(dp1[1][i1][i2][j1], dp1[0][i1][i2][md]);
        }
      }
    }
    REP (j1, W) {
      REPI (j2, j1, W) {
        int i2 = 0;
        REP (i1, H) {
          chmax(i2, i1);
          while (i2 < H) {
            int md = dp1[0][i1][i2][j1];
            int en = dp1[0][i1][i2][md];
            if (en <= j2) break;
            i2++;
          }
          dp2[1][j1][j2][i1] = max(dp2[0][j1][j2][i1], i2);
        }
        REP (i1, H) {
          int md = dp2[0][j1][j2][i1];
          chmax(dp2[1][j1][j2][i1], dp2[0][j1][j2][md]);
        }
      }
    }
    swap(dp1[0], dp1[1]);
    swap(dp2[0], dp2[1]);
  }
}


Measured memory_bytes: 257920