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: Accepted)

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){
	return o<<"("<<p.fs<<","<<p.sc<<")";
}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){
	o<<"{";
	for(const T& v:vc) o<<v<<",";
	o<<"}";
	return o;
}
using ll = long long;
template<class T> using V = vector<T>;
template<class T> using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n-1); }

#ifdef LOCAL
#define show(x) cerr << "LINE" << __LINE__ << " : " << #x << " = " << (x) << endl
#else
#define show(x) true
#endif

int H,W;
V<string> s;
const int X = 187;
int lrd2u[2][X][X][X];
int dul2r[2][X][X][X];

int A[X][X];
bool allEqual(int d,int u,int l,int r){
	int c = A[u][r] - A[u][l] - A[d][r] + A[d][l];
	return c == 0 || c == (u-d)*(r-l);
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);		//DON'T USE scanf/printf/puts !!
	cout << fixed << setprecision(20);
	
	cin >> H >> W;
	s.resize(H);
	rep(i,H) cin >> s[i];
	rep(i,H) rep(j,W){
		if(s[i][j] == '#') A[i+1][j+1] = 1;
	}
	rep(i,H+1) rep(j,W) A[i][j+1] += A[i][j];
	rep(i,H) rep(j,W+1) A[i+1][j] += A[i][j];



	rep(l,W) for(int r=l+1;r<=W;r++){
		lrd2u[0][l][r][H] = H;
		for(int d=H-1;d>=0;d--){
			lrd2u[0][l][r][d] = d;
			if(allEqual(d,d+1,l,r)){
				lrd2u[0][l][r][d] = d + 1;
				if(d != H-1 && s[d][l] == s[d+1][l]) lrd2u[0][l][r][d] = lrd2u[0][l][r][d+1];
			}
		}
	}
	rep(d,H) for(int u=d+1;u<=H;u++){
		dul2r[0][d][u][W] = W;
		for(int l=W-1;l>=0;l--){
			dul2r[0][d][u][l] = l;
			if(allEqual(d,u,l,l+1)){
				dul2r[0][d][u][l] = l+1;
				if(l != W-1 && s[d][l] == s[d][l+1]) dul2r[0][d][u][l] = dul2r[0][d][u][l+1];
			}
		}
	}
	rep(t,100){

		// cerr << " -----------turn =  " << t << endl;
		// rep(l,W) for(int r=l+1;r<=W;r++) rep(d,H){
		// 	cerr << "lrd2u["<<l<<"]["<<r<<"]["<<d<<"] = " << lrd2u[t&1][l][r][d] << endl;
		// }
		// rep(d,H) for(int u=d+1;u<=H;u++) rep(l,W){
		// 	cerr << "dul2r["<<d<<"]["<<u<<"]["<<l<<"] = " << dul2r[t&1][d][u][l] << endl;
		// }

		if(lrd2u[t&1][0][W][0] == H){
			cout << t << endl;
			return 0;
		}

		rep(l,W) for(int r=l+1;r<=W;r++){
			rep(d,H+1){
				lrd2u[1-(t&1)][l][r][d] = lrd2u[t&1][l][r][lrd2u[t&1][l][r][d]];
			}
		}
		rep(d,H) for(int u=d+1;u<=H;u++){
			rep(l,W+1){
				dul2r[1-(t&1)][d][u][l] = dul2r[t&1][d][u][dul2r[t&1][d][u][l]];
			}
		}
		rep(l,W) for(int r=l+1;r<=W;r++){
			rep(d,H+1){
				int u = lrd2u[1-(t&1)][l][r][d];
				chmax(dul2r[1-(t&1)][d][u][l],r);
			}
		}
		rep(d,H) for(int u=d+1;u<=H;u++){
			rep(l,W+1){
				int r = dul2r[1-(t&1)][d][u][l];
				chmax(lrd2u[1-(t&1)][l][r][d],u);
			}
		}
		for(int len=W;len>0;len--){
			rep(l,W){
				int r = l + len;
				if(r > W) break;
				rep(d,H+1){
					chmax(lrd2u[1-(t&1)][l+1][r][d],lrd2u[1-(t&1)][l][r][d]);
					chmax(lrd2u[1-(t&1)][l][r-1][d],lrd2u[1-(t&1)][l][r][d]);
				}
			}
		}
		for(int len=H;len>0;len--){
			rep(d,H){
				int u = d + len;
				if(u > H) break;
				rep(l,W+1){
					chmax(dul2r[1-(t&1)][d+1][u][l],dul2r[1-(t&1)][d][u][l]);
					chmax(dul2r[1-(t&1)][d][u-1][l],dul2r[1-(t&1)][d][u][l]);
				}
			}
		}
		
	}
}