Problem p03056 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#define max(x, y) ((x > y ? x : y))
#define min(x, y) ((x < y ? x : y))
using namespace std;
const int N = 200;
char f[20][N][N][N];
int n, m;
char a[N][N];
int sum[N][N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &a[i][j]);
      if (a[i][j] == '#')
        sum[i][j] = 1;
      sum[i][j] += sum[i - 1][j];
    }
  for (int cnt = 0; cnt < 20; cnt++) {
    for (int i = m; i; i--)
      for (int hr = 1; hr <= n; hr++) {
        int mid = hr;
        for (int hl = hr; hl; hl--) {
          if (cnt) {
            while (
                mid - 1 > hl &&
                min(f[cnt - 1][i][mid][hr], f[cnt - 1][i][hl][mid - 1]) <=
                    min(f[cnt - 1][i][mid - 1][hr], f[cnt - 1][i][hl][mid - 2]))
              mid--;
            if (mid != hl)
              f[cnt][i][hl][hr] =
                  max(f[cnt][i][hl][hr],
                      min(f[cnt - 1][i][mid][hr], f[cnt - 1][i][hl][mid - 1]));

            if (f[cnt - 1][i][hl][hr] + i <= m)
              f[cnt][i][hl][hr] =
                  max(f[cnt][i][hl][hr],
                      f[cnt - 1][i][hl][hr] +
                          f[cnt - 1][i + f[cnt - 1][i][hl][hr]][hl][hr]);
            else
              f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], f[cnt - 1][i][hl][hr]);
            f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], f[cnt - 1][i][hl][hr]);
          } else {
            if ((sum[hr][i] - sum[hl - 1][i]) % (hr - hl + 1) == 0) {
              if (i == m)
                f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], 1);
              else {
                if ((sum[hr][i] - sum[hl - 1][i]) ==
                    (sum[hr][i + 1] - sum[hl - 1][i + 1]))
                  f[cnt][i][hl][hr] =
                      max(f[cnt][i][hl][hr], f[cnt][i + 1][hl][hr] + 1);
                else
                  f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], 1);
              }
            }
          }
        }
      }
    if (f[cnt][1][1][n] == m) {
      printf("%d\n", cnt);
      return 0;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <queue>
#include <set>
#define max(x, y) ((x > y ? x : y))
#define min(x, y) ((x < y ? x : y))
using namespace std;
const int N = 200;
short f[20][N][N][N];
int n, m;
char a[N][N];
int sum[N][N];
int main() {
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      scanf(" %c", &a[i][j]);
      if (a[i][j] == '#')
        sum[i][j] = 1;
      sum[i][j] += sum[i - 1][j];
    }
  for (int cnt = 0; cnt < 20; cnt++) {
    for (int i = m; i; i--)
      for (int hr = 1; hr <= n; hr++) {
        int mid = hr;
        for (int hl = hr; hl; hl--) {
          if (cnt) {
            while (
                mid - 1 > hl &&
                min(f[cnt - 1][i][mid][hr], f[cnt - 1][i][hl][mid - 1]) <=
                    min(f[cnt - 1][i][mid - 1][hr], f[cnt - 1][i][hl][mid - 2]))
              mid--;
            if (mid != hl)
              f[cnt][i][hl][hr] =
                  max(f[cnt][i][hl][hr],
                      min(f[cnt - 1][i][mid][hr], f[cnt - 1][i][hl][mid - 1]));

            if (f[cnt - 1][i][hl][hr] + i <= m)
              f[cnt][i][hl][hr] =
                  max(f[cnt][i][hl][hr],
                      f[cnt - 1][i][hl][hr] +
                          f[cnt - 1][i + f[cnt - 1][i][hl][hr]][hl][hr]);
            else
              f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], f[cnt - 1][i][hl][hr]);
            f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], f[cnt - 1][i][hl][hr]);
          } else {
            if ((sum[hr][i] - sum[hl - 1][i]) % (hr - hl + 1) == 0) {
              if (i == m)
                f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], 1);
              else {
                if ((sum[hr][i] - sum[hl - 1][i]) ==
                    (sum[hr][i + 1] - sum[hl - 1][i + 1]))
                  f[cnt][i][hl][hr] =
                      max(f[cnt][i][hl][hr], f[cnt][i + 1][hl][hr] + 1);
                else
                  f[cnt][i][hl][hr] = max(f[cnt][i][hl][hr], 1);
              }
            }
          }
        }
      }
    if (f[cnt][1][1][n] == m) {
      printf("%d\n", cnt);
      return 0;
    }
  }
  return 0;
}