alexjercan/bugnet huggingface dataset:
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int H, W;
vector<vector<int>> v;
vector<vector<bool>> used;
int solve(int x, int y) {
  int ret = 0, d[] = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    int tx = x + d[i], ty = y + d[i ^ 1];
    if (0 <= tx && tx < W && 0 <= ty && ty < H && v[ty][tx] == 1 &&
        !used[ty][tx]) {
      used[ty][tx] = true;
      ret = max(ret, solve(tx, ty) + 1);
      used[ty][tx] = false;
    }
  }
  return ret;
}
int main() {
  while (cin >> W >> H, H) {
    v = vector<vector<int>>(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> v[i][j];
    }
    used = vector<vector<bool>>(H, vector<bool>(W, false));
    int ret = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (v[i][j] == 0)
          ret = max(ret, solve(j, i));
      }
    }
    cout << ret << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int H, W;
vector<vector<int>> v;
vector<vector<bool>> used;
int solve(int x, int y) {
  int ret = 1, d[] = {1, 0, -1, 0};
  for (int i = 0; i < 4; i++) {
    int tx = x + d[i], ty = y + d[i ^ 1];
    if (0 <= tx && tx < W && 0 <= ty && ty < H && v[ty][tx] == 1 &&
        !used[ty][tx]) {
      used[ty][tx] = true;
      ret = max(ret, solve(tx, ty) + 1);
      used[ty][tx] = false;
    }
  }
  return ret;
}
int main() {
  while (cin >> W >> H, H) {
    v.resize(H, vector<int>(W));
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++)
        cin >> v[i][j];
    }
    used.resize(H, vector<bool>(W));
    int ret = 0;
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        if (v[i][j] == 0)
          ret = max(ret, solve(j, i));
      }
    }
    cout << ret << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
const int MAX_N = 100;
int m, n;
vector<vector<int>> table(MAX_N, vector<int>(MAX_N)),
    used(MAX_N, vector<int>(MAX_N));
int dfs(int y, int x) {
  int res = 0;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny < 0 || nx < 0 || ny >= n || nx >= m)
      continue;
    if (table[ny][nx] == 0 || used[ny][nx])
      continue;
    used[ny][nx] = 1;
    res = max(res, dfs(ny, nx));
    used[ny][nx] = 0;
  }
  return res + 1;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> m >> n, m | n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> table[i][j];
        used[i][j] = 0;
      }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (table[i][j] == 0)
          continue;
        used[i][j] = 1;
        ans = max(ans, dfs(i, j));
        used[i][j] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1 << 30;
const int MOD = 1e9 + 7;
const int dy[] = {1, 0, -1, 0};
const int dx[] = {0, 1, 0, -1};
int m, n;
vector<vector<int>> table, used;
int dfs(int y, int x) {
  int res = 0;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (ny < 0 || nx < 0 || ny >= n || nx >= m)
      continue;
    if (table[ny][nx] == 0 || used[ny][nx])
      continue;
    used[ny][nx] = 1;
    res = max(res, dfs(ny, nx));
    used[ny][nx] = 0;
  }
  return res + 1;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (cin >> m >> n, m | n) {
    table.resize(n, vector<int>(m));
    used.resize(n, vector<int>(m, 0));
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++)
        cin >> table[i][j];
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (table[i][j] == 0)
          continue;
        used[i][j] = 1;
        ans = max(ans, dfs(i, j));
        used[i][j] = 0;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

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

#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int n, m;
bool s[92][92];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int len(int i, int j) {
  if (!s[i][j])
    return 0;
  s[i][j] = false;
  int r = 0;
  rep(k, 4) { r = max(r, len(i + dx[k], j + dy[k])); }
  s[i][j] = true;
  return r + 1;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> n >> m, n | m) {
    rep(i, m + 1) s[n + 1][i] = false;
    rep(i, n + 1) s[i][m + 1] = false;
    rep(i, m) {
      rep(j, n) { cin >> s[j + 1][i + 1]; }
    }
    int l = 0;
    rep(i, m) {
      rep(j, n) { l = max(l, len(j + 1, i + 1)); }
    }
    cout << l << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

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

#define repi(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) for (int i = 0; i < (n); i++)

int n, m;
bool s[92][92];
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int len(int i, int j) {
  if (!s[i][j])
    return 0;
  s[i][j] = false;
  int r = 0;
  rep(k, 4) { r = max(r, len(i + dx[k], j + dy[i])); }
  s[i][j] = true;
  return r + 1;
}

int main() {
  ios::sync_with_stdio(false);

  while (cin >> n >> m, n | m) {
    rep(i, m) s[n][i] = false;
    rep(i, n) s[i][m] = false;
    rep(i, m) {
      rep(j, n) { cin >> s[j + 1][i + 1]; }
    }
    int l = 0;
    rep(i, m) {
      rep(j, n) { l = max(l, len(j + 1, i + 1)); }
    }
    cout << l << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int w, h, ans;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

void solve(int x, int y, int cnt, int m[100][100]) {
  ans = max(ans, cnt);
  for (int i = 0; i < 4; i++) {
    int mx = x + dx[i];
    int my = y + dy[i];
    if (mx < 0 || my < 0 || mx >= w || my >= h)
      continue;
    if (m[my][mx] == 1) {
      m[my][mx] = 0;
      solve(mx, my, cnt + 1, m);
      m[my][mx] = 1;
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int m[100][100] = {0};
    ans = 0;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> m[y][x];
      }
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        solve(x, y, 0, m);
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

int w, h, ans;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

void solve(int x, int y, int cnt, vector<vector<int>> m) {
  ans = max(ans, cnt);
  for (int i = 0; i < 4; i++) {
    int mx = x + dx[i];
    int my = y + dy[i];
    if (mx < 0 || my < 0 || mx >= w || my >= h)
      continue;
    if (m[my][mx] == 1) {
      m[my][mx] = 0;
      solve(mx, my, cnt + 1, m);
      m[my][mx] = 1;
    }
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    vector<vector<int>> m;
    ans = 0;

    for (int y = 0; y < h; y++) {
      vector<int> vc;
      int v;

      for (int x = 0; x < w; x++) {
        cin >> v;
        vc.push_back(v);
      }
      m.push_back(vc);
    }
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        solve(x, y, 0, m);
      }
    }
    cout << ans << endl;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#define INF -1
using namespace std;
void dfs(int, int);
int dist[91][91], y, x, cnt = 0, mx, m, n;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main(void) {
  int i, j, k;
  while (1) {
    cin >> m >> n;
    mx = -1;
    if (m == 0 && n == 0)
      break;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        dist[i][j] = 0;
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> dist[i][j];
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (dist[i][j] == 1) {
          cnt = 0;
          dfs(i, j);
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}
void dfs(int x, int y) {
  int i, j, k;
  for (i = 0; i < 4; i++) {
    if (dist[y + dy[i]][x + dx[i]] == 1 && y + dy[i] <= n && y + dy[i] >= 0 &&
        x + dx[i] <= m && x + dx[i] >= 0) {
      cnt++;
      dist[y + dy[i]][x + dx[i]] = 0;
      mx = max(mx, cnt);
      dfs(x + dx[i], y + dy[i]);
      cnt--;
      dist[y + dy[i]][x + dx[i]] = 1;
    }
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#define INF -1
using namespace std;
void dfs(int, int);
int dist[91][91], y, x, cnt = 0, mx = INF, m, n;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
int main(void) {
  int i, j, k;
  while (1) {
    cin >> m >> n;
    if (m == 0 && n == 0)
      break;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        dist[i][j] = 0;
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        cin >> dist[i][j];
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        if (dist[i][j] == 1) {
          dist[i][j] = 0;
          dfs(i, j);
        }
      }
    }
    cout << mx << endl;
  }
  return 0;
}
void dfs(int y, int x) {
  int i, j, k;
  for (i = 0; i < 4; i++) {
    for (j = 0; j < 4; j++) {
      if (dist[y + dy[i]][x + dx[j]] == 1 && y + dy[i] <= n && y + dy[i] >= 0 &&
          x + dx[j] <= m && x + dx[j] >= 0) {
        cnt++;
        dist[y + dy[i]][x + dx[j]] = 0;
        mx = max(mx, cnt);
        dfs(y + dy[i], x + dx[j]);
        cnt--;
        dist[y + dy[i]][x + dx[j]] = 1;
      }
    }
  }
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
  int maxim = 0;
  maps[y][x] = false;
  REP(i, 4)
  if (y + dy[i] >= 0 && x + dx[i] >= 0 && maps[y + dy[i]][x + dx[i]])
    maxim = max(maxim, search(y + dy[i], x + dx[i]));
  maps[y][x] = true;
  return maxim + 1;
}
int main() {
  // your code goes here
  int m, n;
  while (cin >> m >> n && n) {
    int maxim = 0;
    REP(i, n)
    REP(j, m)
    cin >> maps[i][j];
    REP(i, n)
    REP(j, m)
    if (maps[i][j])
      maxim = max(maxim, search(i, j));
    cout << maxim << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, a) FOR(i, 0, a)
using namespace std;
bool maps[100][100];
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int search(int y, int x) {
  int maxim = 0;
  maps[y][x] = false;
  REP(i, 4)
  if (y + dy[i] && x + dx[i] && maps[y + dy[i]][x + dx[i]])
    maxim = max(maxim, search(y + dy[i], x + dx[i]));
  maps[y][x] = true;
  return maxim + 1;
}
int main() {
  // your code goes here
  int m, n;
  while (cin >> m >> n && n) {
    int maxim = 0;
    REP(i, n)
    REP(j, m)
    cin >> maps[i][j];
    REP(i, n)
    REP(j, m)
    if (maps[i][j])
      maxim = max(maxim, search(i, j));
    cout << maxim << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
struct sta {
  int st[95][95];
  int x, y;
  int n;
};
int main() {
  int m, n;
  while (cin >> m >> n, m || n) {
    sta s, b;
    memset(b.st, 0, sizeof(b.st));
    int i, j, k, l;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> b.st[j][i];
      }
    }

    int ans = 0;
    int ax[] = {1, -1, 0, 0};
    int ay[] = {0, 0, 1, -1};
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        if (b.st[j][i] == 0)
          continue;
        for (k = 1; k <= n; k++) {
          for (l = 1; l <= m; l++) {
            s.st[l][k] = b.st[l][k];
          }
        }
        s.x = j;
        s.y = i;
        s.n = 1;
        s.st[s.x][s.y] = 0;
        queue<sta> q;
        q.push(s);
        while (!q.empty()) {
          s = q.front();
          q.pop();
          ans = max(ans, s.n);
          for (k = 0; k < 4; k++) {
            s.x += ax[k];
            s.y += ay[k];
            if (s.st[s.x][s.y]) {
              s.st[s.x][s.y] = 0;
              s.n++;
              q.push(s);
              s.n--;
              s.st[s.x][s.y] = 1;
            }
            s.x -= ax[k];
            s.y -= ay[k];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct sta {
  int st[95][95];
  int x, y;
  int n;
};
int main() {
  int m, n;
  while (cin >> m >> n, m || n) {
    sta s, b;
    memset(b.st, 0, sizeof(b.st));
    int i, j, k, l;
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        cin >> b.st[j][i];
      }
    }

    int ans = 0;
    int ax[] = {1, -1, 0, 0};
    int ay[] = {0, 0, 1, -1};
    for (i = 1; i <= n; i++) {
      for (j = 1; j <= m; j++) {
        if (b.st[j][i] == 0)
          continue;
        for (k = 1; k <= n; k++) {
          for (l = 1; l <= m; l++) {
            s.st[l][k] = b.st[l][k];
          }
        }
        s.x = j;
        s.y = i;
        s.n = 1;
        queue<sta> q;
        q.push(s);
        while (!q.empty()) {
          s = q.front();
          q.pop();
          s.st[s.x][s.y] = 0;
          ans = max(ans, s.n);
          for (k = 0; k < 4; k++) {
            s.x += ax[k];
            s.y += ay[k];
            if (s.st[s.x][s.y]) {
              s.n++;
              q.push(s);
              s.n--;
            }
            s.x -= ax[k];
            s.y -= ay[k];
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

static const int INF = 1E8;
static const int dx[] = {-1, 0, 1, 0};
static const int dy[] = {0, -1, 0, 1};
int n, m;
int d[92][92][92][92];
bool maze[92][92];

inline bool check(int hx, int hy) {
  return 0 <= hx && hx < n && 0 <= hy && hy < m;
}

void dfs(int hx, int hy, int &cnt) {
  maze[hx][hy] = 0;
  cnt += 1;
  int mx = cnt;
  rep(k, 4) {
    int nx = hx + dx[k];
    int ny = hy + dy[k];
    if (!check(nx, ny))
      continue;
    int tmp = mx;
    if (maze[nx][ny]) {
      dfs(nx, ny, tmp);
      cnt = max(cnt, tmp);
    }
  }
  maze[hx][hy] = 1;
}

int main(int argc, char *argv[]) {
  while (cin >> m >> n, m) {
    rep(i, n) rep(j, m) cin >> maze[i][j];

    int res = 0;
    rep(i, n) rep(j, m) if (maze[i][j]) {
      int cnt = 0;
      dfs(i, j, cnt);
      res = max(res, cnt);
    }
    cout << res << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

static const int INF = 1E8;
static const int dx[] = {-1, 0, 1, 0};
static const int dy[] = {0, -1, 0, 1};
int n, m;
int d[99][99][99][99];
int maze[99][99];

inline bool check(int hx, int hy) {
  return 0 <= hx && hx < m && 0 <= hy && hy < n;
}

int main(int argc, char *argv[]) {
  while (cin >> m >> n, m) {
    rep(i, n) rep(j, m) cin >> maze[i][j];

    rep(i, n) rep(j, m) rep(k, n) rep(l, m) d[i][j][k][l] = INF;
    rep(i, n) rep(j, m) d[i][j][i][j] = 0;
    rep(i, n) rep(j, m) rep(k, 4) {
      int nx = i + dx[k];
      int ny = j + dy[k];
      if (!check(nx, ny))
        continue;
      if (maze[nx][ny])
        d[i][j][nx][ny] = 1;
    }

    rep(k1, n) rep(k2, m) rep(i1, n) rep(i2, m) rep(j1, n) rep(j2, m)
        d[i1][i2][j1][j2] =
            min(d[i1][i2][j1][j2], d[i1][i2][k1][k2] + d[k1][k2][j1][j2]);

    int res = 0;
    rep(i, n) rep(j, m) rep(k, n) rep(l, m) if (d[i][j][k][l] < INF) res =
        max(res, d[i][j][k][l]);
    cout << res << endl;
  }

  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
using namespace std;
int m, n;
int maps[92][92];
int res;
void dfs(int x, int y, int r) {
  int dx[] = {-1, 0, 0, 1};
  int dy[] = {0, -1, 1, 0};
  maps[x][y] = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (maps[nx][ny]) {
      dfs(nx, ny, r + 1);
    }
  }
  res = max(r, res);
  maps[x][y] = 1;
}
int main() {
  while (scanf("%d%d", &m, &n)) {
    if (m == 0 && n == 0)
      break;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++)
        scanf("%d", &maps[i][j]);
    res = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        if (maps[i][j]) {
          dfs(i, j, 0);
        }
      }
    printf("%d\\n", res + 1);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
typedef vector<int> vi;
int m, n;
int dfs(int x, int y, int res, vector<vi> maps) {
  int dx[] = {-1, 0, 0, 1};
  int dy[] = {0, -1, 1, 0};
  maps[x][y] = 0;
  int temp = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i], ny = y + dy[i];
    if (nx >= 0 && nx < n && ny >= 0 && ny < m && maps[nx][ny] == 1) {
      temp = max(temp, dfs(nx, ny, res + 1, maps));
    }
  }
  return max(res, temp);
}
int main() {
  while (scanf("%d%d", &m, &n)) {
    if (m == 0 && n == 0)
      break;
    vector<vi> maps(n, vi(m));
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        scanf("%d", &maps[i][j]);
    int res = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        if (maps[i][j] == 1) {
          res = max(res, dfs(i, j, 1, maps));
        }
      }
    printf("%d\\n", res);
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int field[92][92] = {};
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};
int ans = 0, ANSW = 0;

int dfs(int X, int Y, int n) {
  if (field[X][Y] == 0)
    return n;
  field[X][Y] = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dfs(X + vx[i], Y + vy[i], n + 1));
  }
  field[X][Y] = 1;
  return ans;
}

int main() {
  while (1) {
    ans = 0, ANSW = 0;
    int l, m;
    scanf("%d%d", &l, &m);
    if (l == 0 && m == 0)
      return 0;
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < m; j++) {
        cin >> field[i + 1][j + 1];
      }
    }
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < m; j++) {
        if (field[i + 1][j + 1])
          ANSW = max(ANSW, dfs(i + 1, j + 1, 0));
      }
    }
    printf("%d\\n", ANSW);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>

using namespace std;

int field[92][92] = {};
int vx[4] = {1, -1, 0, 0};
int vy[4] = {0, 0, 1, -1};
int ans = 0, ANSW = 0;

int dfs(int X, int Y, int n) {
  if (field[X][Y] == 0)
    return n;
  field[X][Y] = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dfs(X + vx[i], Y + vy[i], n + 1));
  }
  field[X][Y] = 1;
  return ans;
}

int main() {
  while (1) {
    int l, m;
    scanf("%d%d", &l, &m);
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < m; j++) {
        cin >> field[i + 1][j + 1];
      }
    }
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < m; j++) {
        if (field[i + 1][j + 1])
          ANSW = max(ANSW, dfs(i + 1, j + 1, 0));
      }
    }
    printf("%d\\n", ANSW);
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

void dfs(int x, int y, int cou);
int n, m;
int mas[100][100] = {{0}};
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int ans = 0;

int main() {
  while (cin >> m >> n && n && m) {
    for (int i = 0; i <= n + 1; i++) {
      for (int j = 0; j <= m + 1; j++) {
        if (i >= 1 && j >= 1 && i <= n && j <= m)
          cin >> mas[i][j];
        else
          mas[i][j] = 0;
      }
    }
    ans = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (mas[i][j] == 1) {
          dfs(j, i, 0);
        }
        /*
        for(int k=0;k<n+2;k++){
                for(int l=0;l<m+2;l++){
                        cout << mas[k][l] << " ";
                }
                cout << endl;
        }
        */
      }
    }
    cout << ans << endl;
  }
}

void dfs(int x, int y, int cou) {
  mas[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (mas[ny][nx] == 1) {
      dfs(nx, ny, cou + 1);
    }
  }
  mas[y][x] = 1;
  if (mas[y + 1][x] == 0 && mas[y - 1][x] == 0 && mas[y][x + 1] == 0 &&
      mas[y][x - 1] == 0) {
    ans = max(ans, cou + 1);
  }
  return;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;

void dfs(int x, int y, int cou);
int n, m;
int mas[100][100];
int dx[] = {-1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};
int ans = 0;

int main() {
  while (cin >> m >> n && n && m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cin >> mas[i][j];
      }
    }
    ans = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (mas[i][j] == 1) {
          dfs(i, j, 0);
        }
      }
    }
    cout << ans << endl;
  }
}

void dfs(int x, int y, int cou) {
  if (mas[x][y + 1] == 0 && mas[x][y - 1] == 0 && mas[x + 1][y] == 0 &&
      mas[x - 1][y] == 0) {
    ans = max(ans, cou + 1);
    return;
  }
  mas[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (mas[ny][nx] == 1 && nx >= 0 && ny >= 0 && nx < m && ny < n) {
      dfs(nx, ny, cou + 1);
    }
  }
  mas[y][x] = 1;
  return;
}