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

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

int ary[100][100], ans[100][100]; //[n][m]
bool isaru[100][100];
int tate[4] = {0, 1, 0, -1};
int yoko[4] = {1, 0, -1, 0};
int M, N, cnt;
void ser(int n, int m, int d) {
  if (isaru[n][m]) {
    cnt = max(cnt, d);
    if (ans[n][m] < d)
      ans[n][m] = d;
    isaru[n][m] = false;
    for (int i = 0; i < 4; i++) {
      if (n + tate[i] >= 0 && n + tate[i] < N && m + yoko[i] >= 0 &&
          m + yoko[i] < M) {
        ser(n + tate[i], m + yoko[i], d + 1);
      }
    }
    isaru[n][m] = true;
  }
}

int main() {
  int cntm;
  while (1) {
    cin >> M >> N;
    if (M == 0 && N == 0) {
      break;
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cin >> ary[i][j];
        isaru[i][j] = ary[i][j];
        ans[i][j] = 0;
      }
    }
    cntm = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cnt = 0;
        ser(i, j, 1);
        cntm = max(cntm, cnt);
      }
    }
    cout << cntm << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;

bool ary[100][100]; //[n][m]
int ary2[100][100];
bool hom[100][100];

int func(int M, int N, int Sm, int Sn) {
  if (!ary[Sn][Sm]) {
    return 0;
  }
  pair<int, int> pea;
  queue<pair<int, int>> qu;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      ary2[i][j] = 0;
      hom[i][j] = false;
    }
  }

  hom[Sn][Sm] = true;
  ary2[Sn][Sm] = 1;
  qu.push(make_pair(Sn, Sm));
  while (!qu.empty()) {
    pea = qu.front();
    qu.pop();
    if (pea.second < M - 1 && ary[pea.first][pea.second + 1] &&
        hom[pea.first][pea.second + 1] == false) {
      hom[pea.first][pea.second + 1] = true;
      ary2[pea.first][pea.second + 1] = ary2[pea.first][pea.second] + 1;
      qu.push(make_pair(pea.first, pea.second + 1));
    }
    if (pea.first < N - 1 && ary[pea.first + 1][pea.second] &&
        hom[pea.first + 1][pea.second] == false) {
      hom[pea.first + 1][pea.second] = true;
      ary2[pea.first + 1][pea.second] = ary2[pea.first][pea.second] + 1;
      qu.push(make_pair(pea.first + 1, pea.second));
    }
    if (pea.second > 0 && ary[pea.first][pea.second - 1] &&
        hom[pea.first][pea.second - 1] == false) {
      hom[pea.first][pea.second - 1] = true;
      ary2[pea.first][pea.second - 1] = ary2[pea.first][pea.second] + 1;
      qu.push(make_pair(pea.first, pea.second - 1));
    }
    if (pea.first > 0 && ary[pea.first - 1][pea.second] &&
        hom[pea.first - 1][pea.second] == false) {
      hom[pea.first - 1][pea.second] = true;
      ary2[pea.first - 1][pea.second] = ary2[pea.first][pea.second] + 1;
      qu.push(make_pair(pea.first - 1, pea.second));
    }
  }
  int cnt = -1;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++) {
      if (cnt < ary2[i][j])
        cnt = ary2[i][j];
    }
  }
  return cnt;
}

int main() {
  int M, N;
  int cnt, tmp;
  while (1) {
    cin >> M >> N;
    if (M == 0 && N == 0)
      break;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        cin >> ary[i][j];
      }
    }
    cnt = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        tmp = func(M, N, j, i);

        cout << i << " " << j << "?????´???" << endl; //
        for (int k = 0; k < N; k++) {
          for (int l = 0; l < M; l++) {
            cout << ary2[k][l] << " "; //
          }
          cout << endl; //
        }
        cout << "?????? " << tmp << endl; //
        if (cnt < tmp)
          cnt = tmp;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}