Problem p03056 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
const int maxn = 185 + 3;
int n, m;
int c[maxn][maxn];
int L[maxn][maxn];
int U[maxn][maxn];
int fl[maxn][maxn][maxn];
int fu[maxn][maxn][maxn];
int gl[maxn][maxn][maxn];
int gu[maxn][maxn][maxn];
char ch;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &ch), c[i][j] = (ch == '#');
      L[i][j] = (j == 1 ? 1 : (c[i][j - 1] == c[i][j] ? L[i][j - 1] + 1 : 1));
      U[i][j] = (i == 1 ? 1 : (c[i - 1][j] == c[i][j] ? U[i - 1][j] + 1 : 1));
    }
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= m; ++y) {
      for (int xx = x, Min = 666; xx <= n; ++xx) {
        if (xx > x && c[xx][y] != c[xx - 1][y])
          Min = 0;
        fl[x][y][xx] = Min = std::min(Min, L[xx][y]);
      }
      for (int yy = y, Min = 666; yy <= m; ++yy) {
        if (yy > y && c[x][yy] != c[x][yy - 1])
          Min = 0;
        fu[x][y][yy] = Min = std::min(Min, U[x][yy]);
      }
    }
  for (int ans = 0; "Jumbo tql"; ++ans) {
    if (fl[1][m][n] == m || fu[n][1][m] == n)
      return printf("%d\n", ans), 0;
    memcpy(gl, fl, sizeof(gl));
    memcpy(gu, fu, sizeof(gu));
    for (int x = 1; x <= n; ++x)
      for (int y = 1; y <= m; ++y) {
        for (int xx = x; xx <= n; ++xx)
          fl[x][y][xx] += gl[x][y - gl[x][y][xx]][xx];
        for (int yy = y; yy <= m; ++yy)
          fu[x][y][yy] += gu[x - gu[x][y][yy]][y][yy];
      }
    for (int x = 1; x <= n; ++x)
      for (int y = 1; y <= m; ++y) {
        for (int xx = x; xx <= n; ++xx)
          for (int yy = y - fl[x][y][xx] + 1; yy <= y; ++yy)
            fu[xx][yy][y] = std::max(fu[xx][yy][y], xx - x + 1);
        for (int yy = y; yy <= m; ++yy)
          for (int xx = x - fu[x][y][yy] + 1; xx <= x; ++xx)
            fl[xx][yy][x] = std::max(fl[xx][yy][x], yy - y + 1);
      }
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
const int maxn = 185 + 3;
int n, m;
int c[maxn][maxn];
int L[maxn][maxn];
int U[maxn][maxn];
int fl[maxn][maxn][maxn];
int fu[maxn][maxn][maxn];
int gl[maxn][maxn][maxn];
int gu[maxn][maxn][maxn];
char ch;
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) {
      scanf(" %c", &ch), c[i][j] = (ch == '#');
      L[i][j] = (j == 1 ? 1 : (c[i][j - 1] == c[i][j] ? L[i][j - 1] + 1 : 1));
      U[i][j] = (i == 1 ? 1 : (c[i - 1][j] == c[i][j] ? U[i - 1][j] + 1 : 1));
    }
  for (int x = 1; x <= n; ++x)
    for (int y = 1; y <= m; ++y) {
      for (int xx = x, Min = 666; xx <= n; ++xx) {
        if (xx > x && c[xx][y] != c[xx - 1][y])
          Min = 0;
        fl[x][y][xx] = Min = std::min(Min, L[xx][y]);
      }
      for (int yy = y, Min = 666; yy <= m; ++yy) {
        if (yy > y && c[x][yy] != c[x][yy - 1])
          Min = 0;
        fu[x][y][yy] = Min = std::min(Min, U[x][yy]);
      }
    }
  for (int ans = 0; "Jumbo tql"; ++ans) {
    if (fl[1][m][n] == m || fu[n][1][m] == n)
      return printf("%d\n", ans), 0;
    memcpy(gl, fl, sizeof(gl));
    memcpy(gu, fu, sizeof(gu));
    for (int x = 1; x <= n; ++x)
      for (int y = 1; y <= m; ++y) {
        for (int xx = x; xx <= n; ++xx)
          fl[x][y][xx] += gl[x][y - gl[x][y][xx]][xx];
        for (int yy = y; yy <= m; ++yy)
          fu[x][y][yy] += gu[x - gu[x][y][yy]][y][yy];
      }
    for (int x = 1; x <= n; ++x)
      for (int y = 1; y <= m; ++y) {
        for (int xx = x, X = 1; xx <= n; ++xx, ++X)
          for (int yy = y - fl[x][y][xx] + 1; yy <= y && fu[xx][yy][y] < X;
               ++yy)
            fu[xx][yy][y] = X;
        for (int yy = y, Y = 1; yy <= m; ++yy, ++Y)
          for (int xx = x - fu[x][y][yy] + 1; xx <= x && fl[xx][yy][x] < Y;
               ++xx)
            fl[xx][yy][x] = Y;
      }
  }
  return 0;
}