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: Time Limit Exceeded)

#include <bits/stdc++.h>

using namespace std;

const int max_n = 185, inf = 100;

int n, m;
unsigned char py[max_n][max_n], px[2][max_n][max_n][max_n];
char a[max_n][max_n], dp[max_n][max_n][max_n][max_n];
int sum[max_n][max_n];

int get_sum(int lx, int rx, int y) {
    if (lx == 0) {
        return sum[rx][y];
    }
    return sum[rx][y] - sum[lx - 1][y];
}

int main() {
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%s", a[i]);
        for (int j = 0; j < m; ++j) {
            sum[i][j] = (a[i][j] == '#');
            if (i) {
                sum[i][j] += sum[i - 1][j];
            }
        }
    }
    int q1 = 0, q2 = 1;
    for (int lx = n - 1; lx >= 0; --lx) {
        for (int rx = lx; rx < n; ++rx) {
            for (int ly = m - 1; ly >= 0; --ly) {
                int cur_sum = 0, tot = 0;
                bool ok = true;
                for (int ry = ly; ry < m; ++ry) {
                    if (ok) {
                        cur_sum += get_sum(lx, rx, ry);
                        tot += rx - lx + 1;
                        if (cur_sum != 0 && cur_sum != tot) {
                            ok = false;
                        }
                    }
                    if (ok) {
                        dp[lx][rx][ly][ry] = 0;
                        //cout << lx << " " << rx << " " << ly << " " << ry << ": " << (int) dp[lx][rx][ly][ry] << endl;
                        continue;
                    }
                    char res = inf;
                    if (ly < ry) {
                        int st = ly, to = ry;
                        if (dp[lx][rx][ly][ry - 1] && ly + 1 < ry) {
                            st = py[ly][ry - 1];
                        }
                        if (dp[lx][rx][ly + 1][ry] && ly + 1 < ry) {
                            to = py[ly + 1][ry];
                        }
                        //assert(st >= ly);
                        //assert(to <= ry);
                        for (int y = st; y <= to && y < ry; ++y) {
                            char val = max(dp[lx][rx][ly][y], dp[lx][rx][y + 1][ry]);
                            if (res > val) {
                                res = val;
                                py[ly][ry] = y;
                            }
                        }
                    }
                    dp[lx][rx][ly][ry] = res;
                    if (lx < rx) {
                        res = inf;
                        int st = lx, to = rx;
                        if (dp[lx][rx - 1][ly][ry] && lx + 1 < rx) {
                            st = px[q2][rx - 1][ly][ry];
                        }
                        if (dp[lx + 1][rx][ly][ry] && lx + 1 < rx) {
                            to = px[q1][rx][ly][ry];
                        }
                        for (int x = st; x <= to && x < rx; ++x) {
                            char val = max(dp[lx][x][ly][ry], dp[x + 1][rx][ly][ry]);
                            if (res > val) {
                                res = val;
                                px[q2][rx][ly][ry] = x;
                            }
                        }
                    }
                    dp[lx][rx][ly][ry] = min(dp[lx][rx][ly][ry], res) + 1;
                    //cout << lx << " " << rx << " " << ly << " " << ry << ": " << (int) dp[lx][rx][ly][ry] << endl;
                }
            }
        }
        swap(q1, q2);
    }
    printf("%d\n", dp[0][n - 1][0][m - 1]);
    return 0;
}