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)

#include <bits/stdc++.h>
// iostream is too mainstream
#include <cstdio>
// bitch please
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <cmath>
#include <iomanip>
#include <time.h>
#define dibs reserve
#define OVER9000 1234567890
#define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
#define tisic 47
#define soclose 1e-8
#define chocolate win
// so much chocolate
#define patkan 9
#define ff first
#define ss second
#define abs(x) (((x) < 0)?-(x):(x))
#define uint unsigned int
#define dbl long double
#define pi 3.14159265358979323846
using namespace std;
// mylittledoge

using cat = long long;

#ifdef DONLINE_JUDGE
	// palindromic tree is better than splay tree!
	#define lld I64d
#endif

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(10);
	int H, W;
	cin >> H >> W;
	vector<string> A(H);
	for(int i = 0; i < H; i++) cin >> A[i];
	constexpr int MX = 16;
	vector< vector< vector<uint8_t> > > max_h[MX+1];
	vector< vector< vector<uint8_t> > > max_w[MX+1];
	for(int v = 0; v <= MX; v++) {
		max_h[v].resize(W+1, vector< vector<uint8_t> >(W+1));
		max_w[v].resize(H+1, vector< vector<uint8_t> >(H+1));
		for(int i = 0; i < H; i++) for(int j = i+1; j <= H; j++) {
			max_w[v][i][j].resize(W+1, 0);
			for(int k = 0; k <= W; k++) max_w[v][i][j][k] = k;
		}
		for(int i = 0; i < W; i++) for(int j = i+1; j <= W; j++) {
			max_h[v][i][j].resize(H+1, 0);
			for(int k = 0; k <= H; k++) max_h[v][i][j][k] = k;
		}
	}

	for(int i = 0; i < H; i++) for(int j = W-1; j >= 0; j--) {
		if(j < W-1 && A[i][j] != A[i][j+1]) max_w[0][i][i+1][j] = j+1;
		else max_w[0][i][i+1][j] = max_w[0][i][i+1][j+1];
	}
	for(int i = 0; i < W; i++) for(int j = H-1; j >= 0; j--) {
		if(j < H-1 && A[j][i] != A[j][i+1]) max_h[0][i][i+1][j] = j+1;
		else max_h[0][i][i+1][j] = max_h[0][i][i+1][j+1];
	}
	for(int d = 2; d <= H; d++) for(int i = 0; i <= H-d; i++)
		for(int j = 0; j < W; j++) if(A[i][j] == A[i+1][j])
			max_w[0][i][i+d][j] = min(max_w[0][i+1][i+d][j], max_w[0][i][i+1][j]);
	for(int d = 2; d <= W; d++) for(int i = 0; i <= W-d; i++)
		for(int j = 0; j < H; j++) if(A[j][i] == A[j][i+1])
			max_h[0][i][i+d][j] = min(max_h[0][i+1][i+d][j], max_h[0][i][i+1][j]);

	for(int v = 1; v <= MX; v++) {
		for(int i = 0; i < H; i++) for(int j = 0; j < W; j++) for(int k = j+1; k <= W; k++) {
			int h1 = max_h[v-1][j][k][i];
			if(h1 == i) break;
			int h2 = max_h[v-1][j][k][h1];
			max_w[v][i][h2][j] = max(max_w[v][i][h2][j], (uint8_t)k);
		}
		for(int i = 0; i < H; i++) {
			for(int k = H-1; k > i; k--) for(int j = 0; j < W; j++)
				max_w[v][i][k][j] = max(max_w[v][i][k][j], max_w[v][i][k+1][j]);
			for(int j = i+1; j <= H; j++) for(int l = 0; l < W; l++) {
				int w2 = max_w[v-1][i][j][l];
				max_w[v][i][j][l] = max(max_w[v][i][j][l], max_w[v-1][i][j][w2]);
			}
		}
		for(int i = 0; i < W; i++) for(int j = 0; j < H; j++) for(int k = j+1; k <= H; k++) {
			int w1 = max_w[v-1][j][k][i];
			if(w1 == i) break;
			int w2 = max_w[v-1][j][k][w1];
			max_h[v][i][w2][j] = max(max_h[v][i][w2][j], (uint8_t)k);
		}
		for(int i = 0; i < W; i++) {
			for(int k = W-1; k > i; k--) for(int j = 0; j < H; j++)
				max_h[v][i][k][j] = max(max_h[v][i][k][j], max_h[v][i][k+1][j]);
			for(int j = i+1; j <= W; j++) for(int l = 0; l < H; l++) {
				int h2 = max_h[v-1][i][j][l];
				max_h[v][i][j][l] = max(max_h[v][i][j][l], max_h[v-1][i][j][h2]);
			}
		}
	}

	for(int i = 0; i <= MX; i++) if(max_w[i][0][H][0] == W) {
		cout << i << "\n";
		return 0;
	}
	return 0;
}

// look at my code
// my code is amazing