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

#include <iostream>

using namespace std;

int ban[90][90];
int ban2[90][90];
int n, m;
int Max = 0;

int saiki(int y, int x, int cont) {
  // Xðé
  ban2[y][x] = 0;
  /*
  for(int i = 0; i < n; i++){
          for(int j = 0; j < m; j++){
                  cout << ban2[i][j] << " ";
          }
          cout << endl;
  }
  cout << endl;
*/
  cont++;
  //		getchar();

  // ì¨k¨¨¼Ì

  if (ban2[y + 1][x] == 1 && (y + 1) < n) {
    saiki(y + 1, x, cont);
    ban2[y + 1][x] = 1;
  }
  if (ban2[y - 1][x] == 1 && (y - 1) >= 0) {
    saiki(y - 1, x, cont);
    ban2[y - 1][x] = 1;
  }
  if (ban2[y][x + 1] == 1 && (x + 1) < m) {
    saiki(y, x + 1, cont);
    ban2[y][x + 1] = 1;
  }
  if (ban2[y][x - 1] == 1 && (x - 1) >= 0) {
    saiki(y, x - 1, cont);
    ban2[y][x - 1] = 1;
  }

  if (Max < cont) {
    Max = cont;
  }
  return cont;
}

int main() {
  for (;;) {
    int cont = 0;
    Max = 0;
    cin >> m >> n;

    if (m == 0 && n == 0) {
      break;
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        ban2[i][j] = ban[i][j] = 0;
      }
    }

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> ban[i][j];
        ban2[i][j] = ban[i][j];
      }
    }

    /*
    for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                    cout << ban[i][j];
            }
            cout << endl;
    }
    */

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (ban[i][j] == 1) {
          cont = saiki(i, j, 0);

          for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
              ban2[i][j] = ban[i][j];
            }
          }
        }
      }
    }

    //	saiki(0,0,0);

    cout << Max << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

int w, h;
int main_ban[90][90];
int ans = 0;

class DATA {

public:
  DATA(int xx, int yy, int m_ban[][90], int ss) {
    x = xx;
    y = yy;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        ban[i][j] = m_ban[i][j];
      }
    }
    step = ss;
  }

  int x; // ツ個サツ催敖暗環置
  int y;
  int ban[90][90];
  int step; // ツ催債つスツテツッツプ
};

int main() {
  for (;;) {

    queue<DATA> Q;

    cin >> w;
    cin >> h;

    ans = 0;

    if (w == 0 && h == 0) {
      break;
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> main_ban[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (main_ban[i][j] == 1) {
          DATA start(j, i, main_ban, 0);
          Q.push(start);
        }
      }
    }

    while (!Q.empty()) {
      DATA d = Q.front();
      Q.pop();

      d.ban[d.y][d.x] = 0;

      if (d.ban[d.y + 1][d.x] || d.ban[d.y - 1][d.x] || d.ban[d.y][d.x + 1] ||
          d.ban[d.y][d.x - 1]) {
        if (d.ban[d.y + 1][d.x] == 1) {
          d.ban[d.y + 1][d.x] = 0;
          DATA next(d.x, d.y + 1, d.ban, d.step + 1);
          Q.push(next);
          d.ban[d.y + 1][d.x] = 1;
        }
        if (d.ban[d.y - 1][d.x] == 1 && d.y - 1 >= 0) {
          d.ban[d.y - 1][d.x] = 0;
          DATA next(d.x, d.y - 1, d.ban, d.step + 1);
          Q.push(next);
          d.ban[d.y - 1][d.x] = 1;
        }
        if (d.ban[d.y][d.x + 1] == 1) {
          d.ban[d.y][d.x + 1] = 0;
          DATA next(d.x + 1, d.y, d.ban, d.step + 1);
          Q.push(next);
          d.ban[d.y - 1][d.x] = 1;
        }
        if (d.ban[d.y][d.x - 1] == 1 && d.x - 1 >= 0) {
          d.ban[d.y][d.x - 1] = 0;
          DATA next(d.x - 1, d.y, d.ban, d.step + 1);
          Q.push(next);
          d.ban[d.y - 1][d.x] = 1;
        }
      } else {
        if (ans < d.step) {
          ans = d.step;
        }
      }
    }
    cout << ans + 1 << endl;
  }
}