Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

#define MAX_N 92

int x[MAX_N][MAX_N];
bool z[MAX_N][MAX_N][4];
bool y[MAX_N][MAX_N];
bool t[MAX_N][MAX_N][4];

stack<int> S;

int main() {
  int n, m, c, d, p, q;
  int MAX = 0;
  while (true) {
    MAX = 0;
    memset(z, false, sizeof(z));
    memset(x, 0, sizeof(x));
    cin >> n;
    cin >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> x[i][j];
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (x[i][j] == 1) {
          if (x[i - 1][j] == 1) {
            z[i][j][0] = true;
          }
          if (x[i][j - 1] == 1) {
            z[i][j][1] = true;
          }
          if (x[i + 1][j] == 1) {
            z[i][j][2] = true;
          }
          if (x[i][j + 1] == 1) {
            z[i][j][3] = true;
          }
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        S.push(i * 100 + j);
        memset(y, false, sizeof(y));
        memset(t, false, sizeof(t));
        y[i][j] = true;
        while (!S.empty()) {
          p = 0;
          c = S.top() / 100;
          d = S.top() % 100;
          if ((z[c][d][0] == true && y[c - 1][d] == false) &&
              t[c][d][0] == false) {
            S.push((c - 1) * 100 + d);
            y[c - 1][d] = true;
            t[c][d][0] = true;
            t[c - 1][d][2] = true;
            p++;
          } else if ((z[c][d][1] == true && y[c][d - 1] == false) &&
                     t[c][d][1] == false) {
            S.push(c * 100 + (d - 1));
            y[c][d - 1] = true;
            t[c][d][1] = true;
            t[c][d - 1][3] = true;
            p++;
          } else if ((z[c][d][2] == true && y[c + 1][d] == false) &&
                     t[c][d][2] == false) {
            S.push((c + 1) * 100 + d);
            y[c + 1][d] = true;
            t[c][d][2] = true;
            t[c + 1][d][0] = true;
            p++;
          } else if ((z[c][d][3] == true && y[c][d + 1] == false) &&
                     t[c][d][3] == false) {
            S.push(c * 100 + (d + 1));
            y[c][d + 1] = true;
            t[c][d][3] = true;
            t[c][d + 1][1] = true;
            p++;
          }
          if (p == 0) {
            if (y[c - 1][d] == false) {
              t[c][d][0] = false;
              t[c - 1][d][2] = false;
            }
            if (y[c][d - 1] == false) {
              t[c][d][1] = false;
              t[c][d - 1][3] = false;
            }
            if (y[c + 1][d] == false) {
              t[c][d][2] = false;
              t[c + 1][d][0] = false;
            }
            if (y[c][d + 1] == false) {
              t[c][d][3] = false;
              t[c][d + 1][1] = false;
            }
            S.pop();
            y[c][d] = false;
          }
          q = S.size();
          MAX = max(MAX, q);
        }
      }
    }
    cout << MAX << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;

#define MAX_N 92

int x[MAX_N][MAX_N];
bool z[MAX_N][MAX_N][4];
bool y[MAX_N][MAX_N];
bool t[MAX_N][MAX_N][4];

stack<int> S;

int main() {
  int n, m, c, d, p, q;
  int MAX = 0;
  while (true) {
    MAX = 0;
    memset(z, false, sizeof(z));
    memset(x, 0, sizeof(x));
    cin >> n;
    cin >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        cin >> x[i][j];
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (x[i][j] == 1) {
          if (x[i - 1][j] == 1) {
            z[i][j][0] = true;
          }
          if (x[i][j - 1] == 1) {
            z[i][j][1] = true;
          }
          if (x[i + 1][j] == 1) {
            z[i][j][2] = true;
          }
          if (x[i][j + 1] == 1) {
            z[i][j][3] = true;
          }
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        S.push(i * 100 + j);
        memset(y, false, sizeof(y));
        memset(t, false, sizeof(t));
        y[i][j] = true;
        while (!S.empty()) {
          p = 0;
          c = S.top() / 100;
          d = S.top() % 100;
          if ((z[c][d][0] == true && y[c - 1][d] == false) &&
              t[c][d][0] == false) {
            S.push((c - 1) * 100 + d);
            y[c - 1][d] = true;
            t[c][d][0] = true;
            t[c - 1][d][2] = true;
            p++;
          } else if ((z[c][d][1] == true && y[c][d - 1] == false) &&
                     t[c][d][1] == false) {
            S.push(c * 100 + (d - 1));
            y[c][d - 1] = true;
            t[c][d][1] = true;
            t[c][d - 1][3] = true;
            p++;
          } else if ((z[c][d][2] == true && y[c + 1][d] == false) &&
                     t[c][d][2] == false) {
            S.push((c + 1) * 100 + d);
            y[c + 1][d] = true;
            t[c][d][2] = true;
            t[c + 1][d][0] = true;
            p++;
          } else if ((z[c][d][3] == true && y[c][d + 1] == false) &&
                     t[c][d][3] == false) {
            S.push(c * 100 + (d + 1));
            y[c][d + 1] = true;
            t[c][d][3] = true;
            t[c][d + 1][1] = true;
            p++;
          }
          if (p == 0) {
            if (y[c - 1][d] == false) {
              t[c][d][0] = false;
              t[c - 1][d][2] = false;
            }
            if (y[c][d - 1] == false) {
              t[c][d][1] = false;
              t[c - 1][d][3] = false;
            }
            if (y[c + 1][d] == false) {
              t[c][d][2] = false;
              t[c + 1][d][0] = false;
            }
            if (y[c][d + 1] == false) {
              t[c][d][3] = false;
              t[c - 1][d][1] = false;
            }
            S.pop();
            y[c][d] = false;
          }
          q = S.size();
          MAX = max(MAX, q);
        }
      }
    }
    cout << MAX << endl;
  }
  return 0;
}