Language: C++

//Author: Heltion
//Date: 2019-05-04 
#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 190;
unsigned char dp[2][maxn * maxn * maxn * maxn / 4];
char s[maxn][maxn];
int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;

inline int code(int r1, int c1, int r2, int c2){
	return ch[r1][r2] * totw + cw[c1][c2];
}
unsigned char f(int r1, int c1, int r2, int c2, int d){
	int h = code(r1, c1, r2, c2);
	int rs = sum[r2][c2] - sum[r1 - 1][c2] - sum[r2][c1 - 1] + sum[r1 - 1][c1 - 1];
	if(rs == 0 or rs == (r2 - r1 + 1) * (c2 - c1 + 1)) dp[d][h] = 0;
	
	if(dp[d][h] < 255) return dp[d][h];
	if(d == 0){
		for(int i = r1; i < r2; i += 1){
			int x = f(r1, c1, i, c2, 0);
			if(x >= dp[d][h]) break;
			int y = f(i + 1, c1, r2, c2, 0);
			if(y < dp[d][h]) dp[d][h] = max(x, y) + 1;
		}
		dp[d][h] = min(dp[d][h], f(r1, c1, r2, c2, 1));
	}
	else{
		for(int i = c1; i < c2; i += 1){
			int x = f(r1, c1, r2, i, 1);
			if(x >= dp[d][h]) break;
			int y = f(r1, i + 1, r2, c2, 1);
			if(y < dp[d][h]) dp[d][h] = max(x, y) + 1;
		}
	}
	//cout << d << " " << r1 << " " << c1 << " " << r2 << " " << c2 << endl;
	//cout << (int)dp[d][h] << endl;
	return dp[d][h];
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int H, W;
	cin >> H >> W;
	for(int i = 1; i < maxn; i += 1) LN[i] = i == 1 ? 0 : LN[(i + 1) / 2] + 1;
	for(int i = 1; i <= H; i += 1) cin >> (s[i] + 1);
	for(int i = 1; i <= H; i += 1){
		for(int j = 1; j <= W; j += 1)
			sum[i][j] = sum[i][j - 1] + (s[i][j] == '#');
		for(int j = 1; j <= W; j += 1) sum[i][j] = sum[i][j] + sum[i - 1][j];
	}
	for(int i = 1; i <= H; i += 1)
		for(int j = i; j <= H; j += 1)
			ch[i][j] = toth += 1;
	for(int i = 1; i <= W; i += 1)
		for(int j = i; j <= W; j += 1)
			cw[i][j] = totw += 1;
	memset(dp, -1, sizeof dp);
	cout << (int)f(1, 1, H, W, 0);
	return 0;
}//Author: Heltion
//Date: 2019-05-04 
#include<bits/stdc++.h>
using namespace std;
constexpr int maxn = 190;
unsigned char dp[2][maxn * maxn * maxn * maxn / 4];
char s[maxn][maxn];
int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;

inline int code(int r1, int c1, int r2, int c2){
	return ch[r1][r2] * totw + cw[c1][c2];
}
unsigned char f(int r1, int c1, int r2, int c2, int d){
	int h = code(r1, c1, r2, c2);
	int rs = sum[r2][c2] - sum[r1 - 1][c2] - sum[r2][c1 - 1] + sum[r1 - 1][c1 - 1];
	if(rs == 0 or rs == (r2 - r1 + 1) * (c2 - c1 + 1)) dp[d][h] = 0;
	
	if(dp[d][h] < 255) return dp[d][h];
	if(d == 0){
		for(int i = r1; i < r2; i += 1){
			int x = f(r1, c1, i, c2, 0);
			if(x >= dp[d][h]) break;
			int y = f(i + 1, c1, r2, c2, 0);
			if(y < dp[d][h]) dp[d][h] = max(x, y) + 1;
		}
		dp[d][h] = min(dp[d][h], f(r1, c1, r2, c2, 1));
	}
	else{
		for(int i = c1; i < c2; i += 1){
			int x = f(r1, c1, r2, i, 1);
			if(x >= dp[d][h]) break;
			int y = f(r1, i + 1, r2, c2, 1);
			if(y < dp[d][h]) dp[d][h] = max(x, y) + 1;
		}
	}
	//cout << d << " " << r1 << " " << c1 << " " << r2 << " " << c2 << endl;
	//cout << (int)dp[d][h] << endl;
	return dp[d][h];
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int H, W;
	cin >> H >> W;
	for(int i = 1; i < maxn; i += 1) LN[i] = i == 1 ? 0 : LN[(i + 1) / 2] + 1;
	for(int i = 1; i <= H; i += 1) cin >> (s[i] + 1);
	for(int i = 1; i <= H; i += 1){
		for(int j = 1; j <= W; j += 1)
			sum[i][j] = sum[i][j - 1] + (s[i][j] == '#');
		for(int j = 1; j <= W; j += 1) sum[i][j] = sum[i][j] + sum[i - 1][j];
	}
	for(int i = 1; i <= H; i += 1)
		for(int j = i; j <= H; j += 1)
			ch[i][j] = toth += 1;
	for(int i = 1; i <= W; i += 1)
		for(int j = i; j <= W; j += 1)
			cw[i][j] = totw += 1;
	memset(dp, -1, sizeof dp);
	cout << (int)f(1, 1, H, W, 0);
	return 0;
}

Compiler output:
a.cc:66:15: error: redefinition of 'constexpr const int maxn'
   66 | constexpr int maxn = 190;
      |               ^~~~
a.cc:5:15: note: 'constexpr const int maxn' previously defined here
    5 | constexpr int maxn = 190;
      |               ^~~~
a.cc:67:15: error: redefinition of 'unsigned char dp [2][325802500]'
   67 | unsigned char dp[2][maxn * maxn * maxn * maxn / 4];
      |               ^~
a.cc:6:15: note: 'unsigned char dp [2][325802500]' previously declared here
    6 | unsigned char dp[2][maxn * maxn * maxn * maxn / 4];
      |               ^~
a.cc:68:6: error: redefinition of 'char s [190][190]'
   68 | char s[maxn][maxn];
      |      ^
a.cc:7:6: note: 'char s [190][190]' previously declared here
    7 | char s[maxn][maxn];
      |      ^
a.cc:69:5: error: redefinition of 'int sum [190][190]'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |     ^~~
a.cc:8:5: note: 'int sum [190][190]' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |     ^~~
a.cc:69:22: error: redefinition of 'int LN [190]'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                      ^~
a.cc:8:22: note: 'int LN [190]' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                      ^~
a.cc:69:32: error: redefinition of 'int ch [190][190]'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                ^~
a.cc:8:32: note: 'int ch [190][190]' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                ^~
a.cc:69:48: error: redefinition of 'int toth'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                ^~~~
a.cc:8:48: note: 'int toth' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                ^~~~
a.cc:69:54: error: redefinition of 'int cw [190][190]'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                      ^~
a.cc:8:54: note: 'int cw [190][190]' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                      ^~
a.cc:69:70: error: redefinition of 'int totw'
   69 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                                      ^~~~
a.cc:8:70: note: 'int totw' previously declared here
    8 | int sum[maxn][maxn], LN[maxn], ch[maxn][maxn], toth, cw[maxn][maxn], totw;
      |                                                                      ^~~~
a.cc:71:12: error: redefinition of 'int code(int, int, int, int)'
   71 | inline int code(int r1, int c1, int r2, int c2){
      |            ^~~~
a.cc:10:12: note: 'int code(int, int, int, int)' previously defined here
   10 | inline int code(int r1, int c1, int r2, int c2){
      |            ^~~~
a.cc:74:15: error: redefinition of 'unsigned char f(int, int, int, int, int)'
   74 | unsigned char f(int r1, int c1, int r2, int c2, int d){
      |               ^
a.cc:13:15: note: 'unsigned char f(int, int, int, int, int)' previously defined here
   13 | unsigned char f(int r1, int c1, int r2, int c2, int d){
      |               ^
a.cc:101:5: error: redefinition of 'int main()'
  101 | int main(){
      |     ^~~~
a.cc:40:5: note: 'int main()' previously defined here
   40 | int main(){
      |     ^~~~