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>
#define ll long long
using namespace std;

int main() {
  int h,w;
  cin>>h>>w;
  vector<int> vecx;
  vecx={1,2,4,8,16,32,64,128,256};
  string vec[h];
  string vecb[w];
  vector<int> ans;
  vector<int> ansb;
  int p=0,q=1,r=2,size=0,sizeb=0;
  for(int i=0;i<h;i++){
    q=1;
    p=0;
    r=2;
    cin>>vec[i];
    if(i>0&&vec[i]==vec[i-1]){
      continue;
    }
    for(int j=0;j<w-1;j++){
      if(vec[i][j]!=vec[i][j+1]){
        q++;
      }
    }
    //cout<<q<<" ";
    for(int i=0;i<8;i++){
      if(vecx[i]<q&&q<=vecx[i+1]){
        p=i+1;
      }
    }
    ans.push_back(p);
    size++;
    //cout<<p<<endl;
  }
  if(size==1){
    cout<<ans[0]<<endl;
    return 0;
  }
  int dp[size][size];
  for(int i=0;i<size;i++){
    for(int j=i;j<size;j++){
      if(i==j){
        dp[i][j]=ans[i];
      }
      else if(j==i+1){
        dp[i][j]=max(ans[i],ans[j])+1;
      }
      else{
        dp[i][j]=1e6;
      }
    }
  }
  for(int l=0;l<size-1;l++){
    for(int r=l+1;r<size;r++){
      for(int i=l;i<r;i++){
        dp[l][r]=min(dp[l][r],max(dp[l][i],dp[i+1][r])+1);
      }
    }
  }
  //cout<<dp[0][size-1]<<endl;
  
  string s;
  for(int i=0;i<w;i++){
    s="";
    q=1;
    p=0;
    r=2;
    for(int j=0;j<h;j++){
      s+=vec[j][i];
    }
    vecb[i]=s;
  }
  for(int i=0;i<w;i++){
    q=1;
    p=0;
    r=2;
    if(i>0&&vecb[i]==vecb[i-1]){
      continue;
    }
    for(int j=0;j<h-1;j++){
      if(vecb[i][j]!=vecb[i][j+1]){
        q++;
      }
    }
    //cout<<q<<" ";
    for(int i=0;i<8;i++){
      if(vecx[i]<q&&q<=vecx[i+1]){
        p=i+1;
      }
    }
    ansb.push_back(p);
    sizeb++;
    //cout<<p<<endl;
  }
  if(sizeb==1){
    cout<<ansb[0]<<endl;
    return 0;
  }
  int dpb[sizeb][sizeb];
  for(int i=0;i<sizeb;i++){
    for(int j=i;j<sizeb;j++){
      if(i==j){
        dpb[i][j]=ansb[i];
      }
      else if(j==i+1){
        dpb[i][j]=max(ansb[i],ansb[j])+1;
      }
      else{
        dpb[i][j]=1e6;
      }
    }
  }
  for(int l=0;l<sizeb-1;l++){
    for(int r=l+1;r<sizeb;r++){
      for(int i=l;i<r;i++){
        dpb[l][r]=min(dpb[l][r],max(dpb[l][i],dpb[i+1][r])+1);
      }
    }
  }
  cout<<min(dp[0][size-1],dpb[0][sizeb-1])<<endl;
}