Problem p03056 (C++)

Buggy submission (status: Memory Limit Exceeded):
/****************************************************************
 *	Author: huhao
 *	Email: 826538400@qq.com
 *	Create time: 2020-02-03 16:33:26
 ****************************************************************/
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define fr(i, a, b) for (int i = (a), end_##i = (b); i <= end_##i; i++)
#define fd(i, a, b) for (int i = (a), end_##i = (b); i >= end_##i; i--)
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
const int N = 210, L = 16;
int n, m, s[N][N], f[N][N][N][L + 2];
char c[N][N];
int S(int x, int y, int X, int Y) {
  return s[X][Y] - s[x - 1][Y] - s[X][y - 1] + s[x - 1][y - 1];
}
int main() {
  n = read();
  m = read();
  fr(i, 1, n) scanf("%s", c[i] + 1);
  fr(i, 1, n) fr(j, 1, m) s[i][j] =
      s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (c[i][j] == '#');
  //	fr(i,1,n) fr(j,1,m) printf("%d%c",s[i][j],j==m?'\n':' ');
  fr(st, 0, n - 1) fr(i, 1, n - st) {
    int j = i + st;
    fd(k, m, 1) if (S(i, k, j, k) == 0 || S(i, k, j, k) == st + 1) {
      if (c[i][k] == c[i][k + 1])
        f[i][j][k][0] = f[i][j][k + 1][0];
      else
        f[i][j][k][0] = k;
    }
    else f[i][j][k][0] = k - 1;
    fr(l, 0, L) f[i][j][m + 1][l] = m;
    fr(l, 1, L) fr(k, 1, m) {
      f[i][j][k][l] = f[i][j][f[i][j][k][l - 1] + 1][l - 1];
      int _l = i, _r = j - 1;
      while (_l < _r) {
        int mid = (_l + _r) >> 1;
        if (f[i][mid][k][l - 1] > f[mid + 1][j][k][l - 1])
          _l = mid + 1;
        else
          _r = mid;
      }
      fr(o, std::max(_l - 2, i), std::min(j - 1, _l + 2)) f[i][j][k][l] =
          std::max(f[i][j][k][l],
                   std::min(f[i][o][k][l - 1], f[o + 1][j][k][l - 1]));
    }
    //		fr(k,1,m) fr(l,0,L) printf("%d %d %d %d
    //%d\n",i,j,k,l,f[i][j][k][l]);
  }
  int ans = 0;
  while (f[1][n][1][ans] < m)
    ans++;
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
/****************************************************************
 *	Author: huhao
 *	Email: 826538400@qq.com
 *	Create time: 2020-02-03 16:33:26
 ****************************************************************/
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define fr(i, a, b) for (int i = (a), end_##i = (b); i <= end_##i; i++)
#define fd(i, a, b) for (int i = (a), end_##i = (b); i >= end_##i; i--)
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
const int N = 190, L = 16;
int n, m, s[N][N], f[N][N][N][L + 2];
char c[N][N];
int S(int x, int y, int X, int Y) {
  return s[X][Y] - s[x - 1][Y] - s[X][y - 1] + s[x - 1][y - 1];
}
int main() {
  n = read();
  m = read();
  fr(i, 1, n) scanf("%s", c[i] + 1);
  fr(i, 1, n) fr(j, 1, m) s[i][j] =
      s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + (c[i][j] == '#');
  //	fr(i,1,n) fr(j,1,m) printf("%d%c",s[i][j],j==m?'\n':' ');
  fr(st, 0, n - 1) fr(i, 1, n - st) {
    int j = i + st;
    fd(k, m, 1) if (S(i, k, j, k) == 0 || S(i, k, j, k) == st + 1) {
      if (c[i][k] == c[i][k + 1])
        f[i][j][k][0] = f[i][j][k + 1][0];
      else
        f[i][j][k][0] = k;
    }
    else f[i][j][k][0] = k - 1;
    fr(l, 0, L) f[i][j][m + 1][l] = m;
    fr(l, 1, L) fr(k, 1, m) {
      f[i][j][k][l] = f[i][j][f[i][j][k][l - 1] + 1][l - 1];
      int _l = i, _r = j - 1;
      while (_l < _r) {
        int mid = (_l + _r) >> 1;
        if (f[i][mid][k][l - 1] > f[mid + 1][j][k][l - 1])
          _l = mid + 1;
        else
          _r = mid;
      }
      fr(o, std::max(_l - 2, i), std::min(j - 1, _l + 2)) f[i][j][k][l] =
          std::max(f[i][j][k][l],
                   std::min(f[i][o][k][l - 1], f[o + 1][j][k][l - 1]));
    }
    //		fr(k,1,m) fr(l,0,L) printf("%d %d %d %d
    //%d\n",i,j,k,l,f[i][j][k][l]);
  }
  int ans = 0;
  while (f[1][n][1][ans] < m)
    ans++;
  printf("%d\n", ans);
  return 0;
}