alexjercan/bugnet huggingface dataset:
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstdio>
int e[100][100], n, m;
int ax[4] = {0, 1, 0, -1}, ay[4] = {1, 0, -1, 0};
int dfs(int x, int y) {
  int i, w = 1;
  for (int i = 0; i < 4; i++) {
    int nx = x + ax[i];
    int ny = y + ay[i];
    if (nx < 0 || nx >= n || ny < 0 || ny >= m) {
      continue;
    }
    e[x][y] = 0;
    if (e[nx][ny] == 1) {
      int num = dfs(nx, ny) + 1;
      if (num > w) {
        w = num;
      }
    }
    e[x][y] = 1;
  }
  return w;
}
int main() {
  while (1) {
    int ans = 0;
    scanf("%d %d", &m, &n);
    if (!m) {
      break;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        scanf("%d", e[i] + j);
      }
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        if (e[i][j]) {
          int num = dfs(i, j);
          if (num > ans) {
            ans = num;
          }
        }
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int w[100][100];
int a, b;
int ans(int c, int d) {
  int num = 0;
  w[c][d] = 0;
  if (c > 0 && w[c - 1][d] == 1) {
    num = max(num, ans(c - 1, d));
  }
  if (d > 0 && w[c][d - 1] == 1) {
    num = max(num, ans(c, d - 1));
  }
  if (c < d - 1 && w[c + 1][d] == 1) {
    num = max(num, ans(c + 1, d));
  }
  if (d < c - 1 && w[c][d + 1] == 1) {
    num = max(num, ans(c, d + 1));
  }
  w[c][d] = 1;
  return num + 1;
}
int main() {
  while (1) {
    scanf("%d %d", &a, &b);
    if (!a)
      break;
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        scanf("%d", &w[i][j]);
      }
    }
    int aaa = 0;
    for (int i = 0; i < b; i++) {
      for (int j = 0; j < a; j++) {
        if (w[i][j] == 1)
          aaa = max(aaa, ans(j, i));
      }
    }
    printf("%d\\n", aaa);
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int m, n;
int ba[90][90];

int solve(int x, int y, int nest) {
  // cout << string(nest,' ');
  // printf("%d,%d\\n",x,y);
  ba[x][y] = 0;
  int res = 0;
  REP(k, 4) {
    int xx = x + dx[k];
    int yy = y + dy[k];
    if (xx < 0 || m <= xx || yy < 0 || n <= yy)
      continue;
    if (ba[xx][yy] == 0)
      continue;
    res = max(res, solve(xx, yy, nest + 1));
  }
  ba[x][y] = 1;
  return res + 1;
}

int main() {
  while (cin >> m >> n, m + n) {
    REP(i, n) {
      REP(j, m) { cin >> ba[j][i]; }
    }
    int res = 0;
    REP(y, n)
    REP(x, m)
    if (ba[x][y])
      res = max(res, solve(x, y, 0));
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef vector<vector<bool>> matrix;

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};

int m, n;
int solve(matrix mat, int x, int y, int nest) {
  // cout << string(nest,' ');
  // printf("%d,%d\\n",x,y);
  mat[x][y] = 0;
  int res = 0;
  REP(k, 4) {
    int xx = x + dx[k];
    int yy = y + dy[k];
    if (xx < 0 || m <= xx || yy < 0 || n <= yy)
      continue;
    if (mat[xx][yy] == 0)
      continue;
    res = max(res, solve(mat, xx, yy, nest + 1));
  }
  return res + 1;
}

int main() {
  while (cin >> m >> n, m + n) {
    matrix mat(m);
    REP(i, n) {
      REP(j, m) {
        bool a;
        cin >> a;
        mat[j].push_back(a);
      }
    }
    int res = 0;
    REP(y, n)
    REP(x, m)
    if (mat[x][y])
      res = max(res, solve(mat, x, y, 0));
    cout << res << endl;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int w, h;
int mx;
vector<vector<int>> t;
void rec(int y, int x, int dp) {
  if (t[y][x] == 0)
    return;
  mx = max(mx, dp);
  t[y][x] = 0;
  if (x + 1 < w)
    rec(y, x + 1, dp + 1);
  if (y + 1 < h)
    rec(y + 1, x, dp + 1);
  if (x - 1 >= 0)
    rec(y, x - 1, dp + 1);
  if (y - 1 >= 0)
    rec(y - 1, x, dp + 1);
  t[y][x] = 1;
}

int main() {
  int o;
  while (cin >> w >> h && w || h) {
    t.clear();
    t.resize(h);
    mx = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> o;
        t[i].push_back(o);
      }
    }
    /*
    for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                    cout << t[i][j];
            }
            cout << endl;
    }*/
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (t[i][j])
          rec(i, j, 1);
      }
    }
    cout << mx << endl;
  }
}

Error: TLE

Fixed submission:

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

int w, h;
int mx;

void rec(int y, int x, vector<vector<int>> d, int dp) {
  if (d[y][x] == 0)
    return;
  mx = max(mx, dp);
  d[y][x] = 0;
  if (x + 1 < w)
    rec(y, x + 1, d, dp + 1);
  if (y + 1 < h)
    rec(y + 1, x, d, dp + 1);
  if (x - 1 >= 0)
    rec(y, x - 1, d, dp + 1);
  if (y - 1 >= 0)
    rec(y - 1, x, d, dp + 1);
}

int main() {
  vector<vector<int>> t;
  int o;
  while (cin >> w >> h && w || h) {
    t.clear();
    t.resize(h);
    mx = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> o;
        t[i].push_back(o);
      }
    }
    /*
    for(int i=0;i<h;i++){
            for(int j=0;j<w;j++){
                    cout << t[i][j];
            }
            cout << endl;
    }*/
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (t[i][j])
          rec(i, j, t, 1);
      }
    }
    cout << mx << endl;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

bool kaku[90][90], kouri[90][90];

int dfs(int a, int b) {
  int re = 0, me;
  if (a >= 90 || a < 0 || b >= 90 || b < 0)
    return 0;
  if (kaku[a][b] || !kouri[a][b])
    return 0;
  kaku[a][b] = true;

  me = dfs(a - 1, b);
  re = max(re, me);
  me = dfs(a, b - 1);
  re = max(re, me);
  me = dfs(a, b + 1);
  re = max(re, me);
  me = dfs(a + 1, b);
  re = max(re, me);

  kaku[a][b] = false;
  return re + 1;
}

int main() {
  int m, n, a, ans, me;

  while (1) {
    ans = 0;

    scanf("%d%d", &m, &n);
    if (m + n == 0)
      break;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        scanf("%d", &a);
        kouri[i][j] = (a == 1);
      }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        me = dfs(i, j);
        ans = max(ans, me);
      }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

bool kaku[90][90], kouri[90][90];

int dfs(int a, int b) {
  int re = 0, me;
  if (a >= 90 || a < 0 || b >= 90 || b < 0)
    return 0;
  if (kaku[a - 1][b] || !kouri[a - 1][b])
    return 0;
  kaku[a][b] = true;

  me = dfs(a - 1, b);
  re = max(re, me);
  me = dfs(a, b - 1);
  re = max(re, me);
  me = dfs(a, b + 1);
  re = max(re, me);
  me = dfs(a + 1, b);
  re = max(re, me);

  kaku[a][b] = false;
  return re + 1;
}

int main() {
  int m, n, a, ans, me;

  while (1) {
    ans = 0;

    scanf("%d%d", &m, &n);
    if (m + n == 0)
      break;

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        scanf("%d", &a);
        kouri[i][j] = (a == 1);
      }

    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++) {
        me = dfs(i, j);
        ans = max(ans, me);
      }

    printf("%d\\n", ans - 1);
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

enum { OFF, ON };
int n, m, v[90][90];
int dy[4] = {0, 1, 0, -1};
int dx[4] = {1, 0, -1, 0};

int rec(int y, int x, int broken) {
  int M;
  v[y][x] = OFF;
  broken++;
  M = broken;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (nx < 0 || m <= nx || ny < 0 || n <= ny)
      continue;
    if (v[ny][nx] == OFF)
      continue;
    M = max(M, rec(ny, nx, broken));
  }
  v[y][x] = ON;
  return M;
}

int main() {
  int ans;
  while (cin >> m >> n, m | n) {
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> v[i][j];
    ans = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (v[i][j])
          ans = max(ans, rec(i, j, 0));
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

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

enum { OFF, ON };
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int rec(vector<vector<int>> v, int y, int x, int broken) {
  int n = v.size(), m = v[0].size();
  int M;
  v[y][x] = OFF;
  broken++;
  M = broken;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i];
    int nx = x + dx[i];
    if (nx < 0 || m <= nx || ny < 0 || n <= ny)
      continue;
    if (v[ny][nx] == OFF)
      continue;
    M = max(M, rec(v, ny, nx, broken));
  }
  return M;
}

int main() {
  int m, n, ans;
  vector<vector<int>> v;
  while (cin >> m >> n, m | n) {
    v.resize(n);
    for (int i = 0; i < n; i++)
      v[i].resize(m);
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        cin >> v[i][j];
    ans = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j < m; j++)
        if (v[i][j])
          ans = max(ans, rec(v, i, j, 0));
    cout << ans << endl;
  }
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
int a[92][92], b[92][92];
int f(int x, int y) {
  int mx = 1;
  if (a[x][y] && b[x][y]) {
    b[x][y] = 0;
    mx = max(mx, f(x - 1, y) + 1);
    mx = max(mx, f(x, y - 1) + 1);
    mx = max(mx, f(x + 1, y) + 1);
    mx = max(mx, f(x, y + 1) + 1);
    b[x][y] = -1;
  } else
    mx = 0;
  return mx;
}
int main() {
  int i, j;
  int w, h;
  int mx;
  while (cin >> w && w) {
    cin >> h;
    memset(a, 0, sizeof(a));
    for (i = 1; i < h + 1; i++)
      for (j = 1; j < w + 1; j++)
        cin >> a[j][i];
    mx = 0;
    for (i = 1; i < h + 1; i++) {
      for (j = 1; j < w + 1; j++) {
        memset(b, -1, sizeof(b));
        mx = max(mx, f(j, i));
      }
    }
    cout << mx << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#define mx2(a, b) ((a) > (b) ? a : b)
using namespace std;
int a[92][92], b[92][92];
int f(int x, int y) {
  int mx;
  if (a[x][y] && b[x][y]) {
    b[x][y] = 0;
    mx = 1 + mx2(f(x - 1, y), mx2(f(x, y - 1), mx2(f(x + 1, y), f(x, y + 1))));
    b[x][y] = -1;
  } else
    mx = 0;
  return mx;
}
int main() {
  int i, j;
  int w, h;
  int mx = 0;
  while (cin >> w && w) {
    cin >> h;
    memset(a, 0, sizeof(a));
    for (i = 1; i < h + 1; i++)
      for (j = 1; j < w + 1; j++)
        cin >> a[j][i];
    memset(b, -1, sizeof(b));
    for (i = 1; i < h + 1; i++)
      for (j = 1; j < w + 1; j++)
        mx = mx2(mx, f(j, i));
    cout << mx << endl;
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int w, h, ans;
bool ice[95][95];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
void dfs(int y, int x, int sum) {
  ice[y][x] = 0;
  for (int i = 0; i < 4; i++) {
    int ay = y + dy[i];
    int ax = x + dx[i];
    if (ice[ay][ax] == 1)
      dfs(ay, ax, sum + 1);
  }
  ice[y][x] = 1;
  if (ice[y - 1][x] == 0 && ice[y + 1][x] == 0 && ice[y][x - 1] == 0 &&
      ice[y][x + 1] == 0) {
    ans = max(ans, sum + 1);
  }
}
int main() {
  while (scanf("%d %d", &w, &h), w != 0 && h != 0) {
    fill_n(*ice, 95 * 95, 0);
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++)
        scanf("%d", &ice[i][j]);
    }
    ans = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (ice[i][j] == 1)
          dfs(i, j, 0);
      }
    }
    printf("%d\\n", ans);
  }
  return (0);
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int w, h, ans, i, j, k;
bool ice[90][90];
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};
int dfs(int y, int x) {
  int ret = 0;
  if (ice[y - 1][x] == 0 && ice[y + 1][x] == 0 && ice[y][x - 1] == 0 &&
      ice[y][x + 1] == 0)
    return 1;
  ice[y][x] = 0;
  for (k = 0; k < 4; k++) {
    int ay = y + dy[k];
    int ax = x + dx[k];
    if (x < 0 || y < 0 || x >= w || y >= h || ice[ay][ax] == 0)
      continue;
    ret = dfs(ay, ax) + 1;
  }
  ice[y][x] = 1;
  return ret;
}
int main() {
  int ma = 0;
  while (scanf("%d %d", &w, &h), w != 0 && h != 0) {
    ans = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++)
        scanf("%d", &ice[i][j]);
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (ice[i][j] == 1)
          ma = max(ma, dfs(i, j));
      }
    }
    printf("%d\\n", ma);
  }
  return (0);
}
Problem p00458 (C++) — buggy submission, verdict: Runtime Error

#include "stdio.h"

int W, H;
int map[90][90] = {};
int place[8500] = {};
int use[8500] = {};
int m[4] = {};
int ans = 0;

void Check(int x[], int p, int num) {
  use[p] = 0;
  for (int i = 0; i < 4; i++) {
    if (use[p + m[i]] == true) {
      //	printf("%d??????%d????§??????????\\n", p, p + m[i]);
      if (ans < num + 1)
        ans = num + 1;
      Check(x, p + m[i], num + 1);
    }
  }
  // printf("\\n");
  use[p] = 1;
  return;
}

int main() {
  scanf("%d", &W);
  scanf("%d", &H);
  while (!(W == 0)) {
    ans = 0;
    for (int i = 0; i < W * H + 400; i++) {
      place[i] = 0;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        map[i][j] = false;
        scanf("%d", &map[i][j]);
        //	place[(i + 1)*(W + 2) + j + 1] = 0;
        place[(i + 1) * (W + 2) + j + 1] = map[i][j];
      }
    }
    m[0] = 1;
    m[1] = W + 2;
    m[2] = -1;
    m[3] = -W - 2;
    for (int i = 0; i < W * H + 400; i++) {
      for (int j = 0; j < W * H + 400; j++) {
        use[j] = place[j];
      }
      if (place[i] == 1)
        Check(m, i, 1);
    }
    printf("%d\\n", ans);
    scanf("%d", &W);
    scanf("%d", &H);
  }
  return 0;
}

Error: 0

Fixed submission:

#include "stdio.h"

int W, H;
int map[90][90] = {};
int place[8500] = {};
int use[8500] = {};
int m[4] = {};
int ans = 0;

void Check(int x[], int p, int num) {
  use[p] = false;
  for (int i = 0; i < 4; i++) {
    if (use[p + m[i]] == true) {
      //	printf("%d??????%d????§??????????\\n", p, p + m[i]);
      if (ans < num + 1)
        ans = num + 1;
      Check(x, p + m[i], num + 1);
    }
  }
  // printf("\\n");
  use[p] = true;
  return;
}

int main() {
  scanf("%d", &W);
  scanf("%d", &H);
  while (!W == 0) {
    ans = 0;

    for (int i = 0; i < W * H * 400; i++) {
      place[i] = 0;
    }
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        map[i][j] = false;
        scanf("%d", &map[i][j]);
        //	place[(i + 1)*(W + 2) + j + 1] = 0;
        place[(i + 1) * (W + 2) + j + 1] = map[i][j];
      }
    }
    m[0] = 1;
    m[1] = W + 2;
    m[2] = -1;
    m[3] = -W - 2;
    for (int i = 0; i < W * H + 400; i++) {
      for (int j = 0; j < W * H + 400; j++) {
        use[j] = place[j];
      }
      if (place[i] == 1)
        Check(m, i, 1);
    }
    printf("%d\\n", ans);
    scanf("%d", &W);
    scanf("%d", &H);
  }
  return 0;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dfs(vector<vector<int>> &vi, int i, int j);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m, n;
  while (cin >> m >> n && m != 0 && n != 0) {
    vector<vector<int>> ice(m, vector<int>(n));
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> ice[j][i];
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (ice[j][i] == 1) {
          ans = max(ans, dfs(ice, j, i));
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}

int dfs(vector<vector<int>> &vi, int i, int j) {
  int res = 0;
  vi[i][j] = 0;
  for (int k = 0; k < 4; ++k) {
    int nx = i + dx[k];
    int ny = j + dy[k];
    if (0 <= nx && nx < vi.size() && 0 <= ny && ny < vi[i].size() &&
        vi[nx][ny] == 1) {
      res = max(res, dfs(vi, nx, ny));
    }
  }
  vi[i][j] = 1;
  return res + 1;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
int dfs(vector<vector<int>> vi, int i, int j, int sum);
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int m, n;
  while (cin >> m >> n && m != 0 && n != 0) {
    vector<vector<int>> ice(m, vector<int>(n));
    int ans = 0;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        cin >> ice[j][i];
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        if (ice[j][i] == 1) {
          ans = max(ans, dfs(ice, j, i, 0));
        }
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}

int dfs(vector<vector<int>> vi, int i, int j, int sum) {
  int res = 0;
  vi[i][j] = 0;
  for (int k = 0; k < 4; ++k) {
    int nx = i + dx[k];
    int ny = j + dy[k];
    if (0 <= nx && nx < vi.size() && 0 <= ny && ny < vi[i].size() &&
        vi[nx][ny] == 1) {
      res = max(sum, dfs(vi, nx, ny, sum));
    }
  }
  vi[i][j] = 1;
  return res + 1;
}
Problem p00458 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;

int w, h, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
bool field[100][100];

int dfs(int x, int y) {
  field[x][y] = false;

  int temp = 0;
  for (int i = 0; i < 4; ++i) {
    int tx = x + dx[i], ty = y + dy[i];
    if (!field[tx][ty])
      continue;
    temp = max(temp, dfs(tx, ty));
  }

  field[x][y] = true;
  return temp + 1;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  while (cin >> w >> h, w) {
    fill(field[0], field[100], 0);

    for (int y = 1; y <= h; ++y) {
      for (int x = 1; x <= w; ++x) {
        cin >> field[x][y];
      }
    }

    int ans = 0;
    for (int y = 1; y <= h; ++y) {
      for (int x = 1; x <= w; ++x) {
        if (!field[x][y])
          continue;

        ans = max(ans, dfs(x, y));
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define int long long
#define LL long long
#define ULL unsigned long long

const LL LL_INF = ~(ULL)0 >> 1;

struct P {
  vector<bool> grid[92];
  int x, y, count;
  P() {}
  P(int x, int y, int count, vector<bool> *grid) {
    this->x = x;
    this->y = y;
    this->count = count;

    for (int i = 0; i < 92; ++i) {
      this->grid[i] = vector<bool>(92);
      for (int j = 0; j < 92; ++j) {
        this->grid[i][j] = grid[i][j];
      }
    }

    this->grid[x][y] = false;
  }
};

int m, n, dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<bool> grid[92];

signed main() {
  while (cin >> m >> n, m) {
    for (int y = 0; y < 92; ++y) {
      grid[y] = vector<bool>(92);
    }

    for (int y = 1; y <= n; ++y) {
      for (int x = 1; x <= m; ++x) {
        bool temp;
        cin >> temp;
        grid[x][y] = temp;
      }
    }

    int max = 0;
    for (int y = 1; y <= n; ++y) {
      for (int x = 1; x <= m; ++x) {
        if (!grid[x][y])
          continue;

        stack<P> s;
        s.push(P(x, y, 1, grid));

        while (s.size()) {
          P p = s.top();
          s.pop();
          if (max < p.count)
            max = p.count;

          for (int i = 0; i < 4; ++i) {
            int tx = p.x + dx[i], ty = p.y + dy[i];
            if (!p.grid[tx][ty])
              continue;

            p.grid[tx][ty] = false;
            s.push(P(tx, ty, p.count + 1, p.grid));
          }
        }
      }
    }

    cout << max << endl;
  }

  return 0;
}