Problem:
Score : 1000 points

Problem Statement

Note the unusual memory limit.

For a rectangular grid where each square is painted white or black, we define its complexity as follows:

If all the squares are black or all the squares are white, the complexity is 0.

Otherwise, divide the grid into two subgrids by a line parallel to one of the sides of the grid, and let c_1 and c_2 be the complexities of the subgrids. There can be multiple ways to perform the division, and let m be the minimum value of \max(c_1, c_2) in those divisions. The complexity of the grid is m+1.

You are given a grid with H horizontal rows and W vertical columns where each square is painted white or black.
HW characters from A_{11} to A_{HW} represent the colors of the squares.
A_{ij} is # if the square at the i-th row from the top and the j-th column from the left is black, and A_{ij} is . if that square is white.

Find the complexity of the given grid.

Constraints

1 \leq H,W \leq 185

A_{ij} is # or ..

Input

Input is given from Standard Input in the following format:

H W
A_{11}A_{12}...A_{1W}
:
A_{H1}A_{H2}...A_{HW}

Output

Print the complexity of the given grid.

Sample Input 1

3 3
...
.##
.##

Sample Output 1

2

Let us divide the grid by the boundary line between the first and second columns.
The subgrid consisting of the first column has the complexity of 0, and the subgrid consisting of the second and third columns has the complexity of 1, so the whole grid has the complexity of at most 2.

Sample Input 2

6 7
.####.#
#....#.
#....#.
#....#.
.####.#
#....##

Sample Output 2

4

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2,abm,fma,popcnt,mmx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
const char nl = '\n';
using namespace std;
typedef long long ll;
typedef long double ld;
typedef complex<ld> pt;
const int MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
const int N = 186, L = 16;

int n, m;
string grid[N];
int pref[N][N];
unsigned char dp[N][N][N][L];
const unsigned char NFILL = 255, BAD = 252;

void init() {
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + (grid[i-1][j-1] == '#');
    }
  }
  memset(dp, NFILL, sizeof dp);
}

inline bool mono(int b, int t, int l, int r) {
  int area = pref[t+1][r+1] - pref[b][r+1] - pref[t+1][l] + pref[b][l];
  return area == 0 || area == (r-l+1)*(t-b+1);
}

int solve(int b, int t, int l, int k) {
  if (l >= BAD) return -2;
  if (l < 0) return -2;
  if (l >= m) return m-1;
  if (dp[b][t][l][k] != NFILL) return dp[b][t][l][k] >= BAD ? -1 : dp[b][t][l][k];
  if (k == 0) {
    int lb = l, rb = m-1, ans = -1;
    while (lb <= rb) {
      int mid = (lb+rb)/2;
      if (mono(b, t, l, mid)) {
        ans = mid;
        lb = mid+1;
      } else rb = mid-1;
    }
    dp[b][t][l][k] = (ans < 0 ? BAD : ans);
    return ans;
  }
  int ans = solve(b, t, solve(b, t, l, k-1)+1, k-1);
  if (b < t) {
    int lb = b, rb = t-1, bsplit = t-1;
    while (lb <= rb) {
      int mid = (lb+rb)/2;
      //ans = max(ans, min(solve(b, mid, l, k-1), solve(mid+1, t, l, k-1)));
      if (solve(b, mid, l, k-1) >= solve(mid+1, t, l, k-1)) {
        bsplit = mid;
        lb = mid+1;
      } else rb = mid-1;
    }
    for (int i = max(b, bsplit-2); i <= min(t-1, bsplit+2); i++) {
      ans = max(ans, min(solve(b, i, l, k-1), solve(i+1, t, l, k-1)));
    }
  }
  //cerr << b << " " << t << " " << l << " " << k << " -> " << ans << nl;
  dp[b][t][l][k] = (ans < 0 ? BAD : ans);
  return ans;
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> grid[i];
  }
  init();
  for (int k = 0; k < L; k++) {
    if (solve(0, n-1, 0, k) >= m-1) {
      cout << k << nl;
      return 0;
    }
  }
  return 0;
}