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

// AOJ 1160
#include <iostream>
#define rep(i, a) for (int i = 0; i != (a); ++i)

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

int w, h;
bool c[50][50];

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

  rep(i, 8) {
    int nx = x + dx[i], ny = y + dy[i];

    if (nx >= 0 && nx < w && ny >= 0 && ny < h && c[ny][nx])
      dfs(ny, nx);
  }

  return;
}

int main() {
  while (std::cin >> w >> h, w | h) {
    rep(i, h) rep(j, w) std::cin >> c[i][j];

    int ans = 0;
    rep(i, h) rep(j, w) if (c[i][j]) dfs(i, j), ++ans;

    std::cout << ans << std::endl;
  }

  return 0;
}

Error: 0

Fixed submission:

// AOJ 1160
#include <cstdio>
#define rep(i, a) for (int i = 0; i != (a); ++i)

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

int w, h;
bool c[50][50];

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

  rep(i, 8) {
    int nx = x + dx[i], ny = y + dy[i];

    if (nx >= 0 && nx < w && ny >= 0 && ny < h && c[ny][nx])
      dfs(ny, nx);
  }

  return;
}

int main() {
  while (scanf("%d%d", &w, &h), w | h) {
    rep(i, h) rep(j, w) scanf("%d", &c[i][j]);

    int ans = 0;
    rep(i, h) rep(j, w) if (c[i][j]) dfs(i, j), ++ans;

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

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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io                                                               \\
  fstream cin("input.txt");                                                    \\
  ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main() {
  debug_io int w, h;
  while (cin >> w >> h, w + h) {
    int tile[55][55];
    REP(i, h) {
      REP(j, w) { cin >> tile[i][j]; }
    }

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

    int ans = 0;
    REP(i, h) {
      REP(j, w) {
        if (tile[i][j] == 1) {
          vector<pair<int, int>> bfs;
          bfs.push_back({i, j});

          while (bfs.size()) {
            auto now = bfs.front();
            bfs.erase(bfs.begin());
            int x = now.second;
            int y = now.first;
            if (tile[y][x] == 2) {
              continue;
            }
            tile[y][x] = 2;
            REP(k, 8) {
              int nx = max(0, min(x + dx[k], w - 1));
              int ny = max(0, min(y + dy[k], h - 1));
              if (tile[ny][nx] == 1) {
                bfs.push_back({ny, nx});
              }
            }
          }
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; ++i)

using namespace std;

#ifdef _DEBUG
#define debug_io                                                               \\
  fstream cin("input.txt");                                                    \\
  ofstream cout("output.txt");
#else
#define debug_io ;
#endif

int main() {
  debug_io int w, h;
  while (cin >> w >> h, w + h) {
    int tile[55][55];
    REP(i, h) {
      REP(j, w) { cin >> tile[i][j]; }
    }

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

    int ans = 0;
    REP(i, h) {
      REP(j, w) {
        if (tile[i][j] == 1) {
          vector<pair<int, int>> bfs;
          bfs.push_back({i, j});

          while (bfs.size()) {
            auto now = bfs.front();
            bfs.erase(bfs.begin());
            int x = now.second;
            int y = now.first;
            tile[y][x] = 2;
            REP(k, 8) {
              int nx = max(0, min(x + dx[k], w - 1));
              int ny = max(0, min(y + dy[k], h - 1));
              if (tile[ny][nx] == 1) {
                bfs.push_back({ny, nx});
              }
            }
          }
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
               {1, 0},   {-1, 1}, {0, 1},  {1, 1}};

void func(int **a, int s, int t, int i, int j) {
  if (a[i][j] == 0) {
    return;
  }
  a[i][j] = 0;

  for (int k = 0; k < 8; k++) {
    if ((i + d[k][0] >= 0 && i + d[k][0] < t) &&
        (j + d[k][1] >= 0 && j + d[k][1] < s)) {
      func(a, s, t, i + d[k][0], j + d[k][1]);
    }
  }

  return;
}

int main(int argc, char **argv) {
  int s, t, **a, c, t2, s2;

  while (1) {
    cin >> s >> t;
    if (s == 0 && t == 0) {
      break;
    } else if (s == 1 || t == 1) {
      t2 = t + 1;
      s2 = s + 1;
    } else {
      t2 = t;
      s2 = s;
    }
    a = (int **)calloc(t2, sizeof(int));
    for (int i = 0; i < t; i++) {
      a[i] = (int *)calloc(s2, sizeof(int));
      for (int j = 0; j < s; j++) {
        cin >> c;
        a[i][j] = c;
      }
    }
    c = 0;

    for (int i = 0; i < t; i++) {
      for (int j = 0; j < s; j++) {
        if (a[i][j] == 1) {
          c++;
          func(a, s, t, i, j);
        }
      }
    }
    cout << c << endl;

    for (int i = 0; i < t2; i++) {
      free(a[i]);
    }
    free(a);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <stdlib.h>

using namespace std;

int d[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0},
               {1, 0},   {-1, 1}, {0, 1},  {1, 1}};

void func(int **a, int s, int t, int i, int j) {
  if (a[i][j] == 0) {
    return;
  }
  a[i][j] = 0;

  for (int k = 0; k < 8; k++) {
    if ((i + d[k][0] >= 0 && i + d[k][0] < t) &&
        (j + d[k][1] >= 0 && j + d[k][1] < s)) {
      func(a, s, t, i + d[k][0], j + d[k][1]);
    }
  }

  return;
}

int main(int argc, char **argv) {
  int s, t, **a, c, t2, s2;

  while (1) {
    cin >> s >> t;
    if (s == 0 && t == 0) {
      break;
    } else if (s == 1 || t == 1) {
      t2 = t + 1;
      s2 = s + 1;
    } else {
      t2 = t;
      s2 = s;
    }
    a = (int **)calloc(t2, sizeof(int));
    for (int i = 0; i < t; i++) {
      a[i] = (int *)calloc(s2, sizeof(int));
      for (int j = 0; j < s; j++) {
        cin >> c;
        a[i][j] = c;
      }
    }
    c = 0;

    for (int i = 0; i < t; i++) {
      for (int j = 0; j < s; j++) {
        if (a[i][j] == 1) {
          c++;
          func(a, s, t, i, j);
        }
      }
    }
    cout << c << endl;

    for (int i = 0; i < s2; i++) {
      free(a[i]);
    }
    free(a);
  }

  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int m[51][51];
int w, h;
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void dfs(int x, int y) {
  m[y][x] = 0;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (nx < 0 || ny < 0 || nx >= w || ny >= h)
      continue;
    else if (m[ny][nx] == 1)
      dfs(nx, ny);
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int isl = 0;

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

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (m[i][j] == 1) {
          isl++;
          dfs(j, i);
        }

    cout << isl << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int m[51][51];
int w, h;
const int dx[] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int dy[] = {-1, -1, -1, 0, 0, 1, 1, 1};

void dfs(int x, int y) {
  m[y][x] = 0;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];

    if (nx < 0 || ny < 0 || nx >= w || ny >= h)
      continue;
    else if (m[nx][ny] == 1)
      dfs(nx, ny);
  }
}

int main() {
  while (cin >> w >> h, w || h) {
    int isl = 0;

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

    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        if (m[i][j] == 1) {
          dfs(i, j);
          isl++;
        }

    cout << isl << endl;
  }

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

#include <cstdio>
#include <cstring>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
int Map[52][52];
bool used[52][52];
const int dir[][2] = {{-1, 0}, {-1, 1}, {0, 1},  {1, 1},
                      {1, 0},  {1, -1}, {0, -1}, {-1, -1}};
int w, h;

void dfs(int row, int col) {
  used[row][col] = true;
  rep(k, 8) {
    int nr = row + dir[k][0];
    int nc = col + dir[k][1];
    if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 ||
        used[nr][nc])
      continue;
    dfs(nr, nc);
  } // end rep
}

int main() {
  while (scanf("%d %d", &w, &h) && w && h) {
    memset(used, false, sizeof(used));
    rep(i, h) {
      rep(j, w) { scanf(" %d", Map[i] + j); } // end rep
    }                                         // end rep
    int cnt = 0;
    rep(i, h) {
      rep(j, w) {
        if (Map[i][j] == 1 && !used[i][j]) {
          dfs(i, j);
          cnt++;
        } // end if
      }   // end rep
    }     // end rep
    printf("%d\\n", cnt);
  } // end loop

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

typedef pair<int, int> P;
const int dir[][2] = {{-1, 0}, {-1, 1}, {0, 1},  {1, 1},
                      {1, 0},  {1, -1}, {0, -1}, {-1, -1}};
int w, h;
int Map[52][52];
bool used[52][52];

void bfs(int row, int col) {
  queue<P> que;
  que.push(P(row, col));
  while (!que.empty()) {
    P curr = que.front();
    que.pop();
    int cr = curr.first, cc = curr.second;
    used[cr][cc] = true;
    rep(k, 8) {
      int nr = cr + dir[k][0];
      int nc = cc + dir[k][1];
      if (nr < 0 || nc < 0 || nr >= h || nc >= w || Map[nr][nc] == 0 ||
          used[nr][nc])
        continue;
      que.push(P(nr, nc));
    } // end rep
  }   // end while
}

int main() {
  while (scanf("%d %d", &w, &h) && w && h) {
    memset(used, false, sizeof(used));
    rep(i, h) {
      rep(j, w) { scanf(" %d", Map[i] + j); } // end rep
    }                                         // end rep
    int cnt = 0;
    rep(i, h) {
      rep(j, w) {
        if (Map[i][j] == 1 && !used[i][j]) {
          bfs(i, j);
          cnt++;
        } // end if
      }   // end rep
    }     // end rep
    printf("%d\\n", cnt);
  } // end loop

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

#include <cstdio>
#define f(n) for (int i = 0; i < n; i++)
#define s(w) scanf("%d", &w)
int a, n, m, d[99][99];
int u[16] = {-1, -1, -1, 0, 0, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 1};
void k(int x, int y) {
  if (d[x][y]) {
    d[x][y] = 0;
    f(8) k(x + u[i], y + u[i + 8]);
  }
}
int main() {
  a = 0;
  s(n);
  s(m);
  if (n == 0)
    return 0;
  f(9801) d[0][i] = 0;
  f(n * m) s(d[i / n + 1][i % n + 1]);
  f(n * m) if (d[i / n + 1][i % n + 1]) a++, k(i / n + 1, i % n + 1);
  printf("%d\\n", a);
  main();
}

Error: TLE

Fixed submission:

#include <cstdio>
#define f(n) for (int i = 0; i < n; i++)
#define s(w) scanf("%d", &w)
int a, n, m, d[99][99];
int u[16] = {-1, -1, -1, 0, 0, 1, 1, 1, -1, 0, 1, -1, 1, -1, 0, 1};
void k(int x, int y) {
  if (d[x][y]) {
    d[x][y] = 0;
    f(8) k(x + u[i], y + u[i + 8]);
  }
}
int main() {
  a = 0;
  s(n);
  s(m);
  f(9801) d[0][i] = 0;
  f(n * m) s(d[i / n + 1][i % n + 1]);
  f(n * m) if (d[i / n + 1][i % n + 1]) a++, k(i / n + 1, i % n + 1);
  printf("%d\\n", a);
  main();
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stack>
using namespace std;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int f[m][n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> f[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (f[i][j] == 1) {
          ans++;
          stack<int> xs, ys;
          xs.push(j);
          ys.push(i);
          while (!xs.empty()) {
            int x = xs.top(), y = ys.top();
            xs.pop();
            ys.pop();
            f[y][x] = 0;
            for (int k = 0; k < 8; ++k) {
              int nx = x + dx[k], ny = y + dy[k];
              if (0 <= nx && nx < n && 0 <= y && y < m && f[ny][nx] == 1) {
                xs.push(nx);
                ys.push(ny);
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
int dx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int dy[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int f[m][n];
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> f[i][j];
      }
    }

    int ans = 0;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (f[i][j] == 1) {
          ans++;
          queue<int> xs, ys;
          xs.push(j);
          ys.push(i);
          while (!xs.empty()) {
            int x = xs.front(), y = ys.front();
            xs.pop();
            ys.pop();
            f[y][x] = 0;
            for (int k = 0; k < 8; ++k) {
              int nx = x + dx[k], ny = y + dy[k];
              if (0 <= nx && nx < n && 0 <= y && y < m && f[ny][nx] == 1) {
                xs.push(nx);
                ys.push(ny);
              }
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error


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

int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, W,
    H;
bool mem[50][50];

int dfs(int x, int y) {
  if (!mem[y][x])
    return 0;
  mem[y][x] = 0;
  for (int i = 0; i < 8; i++) {
    int tx = x + dx[i];
    int ty = y + dy[i];
    if (tx >= 0 && tx < W && ty >= 0 && ty < H && mem[ty][tx])
      dfs(tx, ty);
  }
  return 1;
}

int main() {
loop:
  int ans = 0;
  for (int y = 0; y < 50; y++)
    for (int x = 0; x < 50; x++)
      mem[y][x] = 0;
  cin >> W >> H;
  if (!W)
    return 0;
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      cin >> mem[y][x];
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      ans += dfs(x, y);
  cout << ans << endl;
  goto loop;
}

Error: 0

Fixed submission:


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

int dx[] = {0, 0, 1, -1, 1, -1, 1, -1}, dy[] = {1, -1, 0, 0, 1, -1, -1, 1}, W,
    H;
bool mem[50][50];

int bfs(int sx, int sy) {
  if (!mem[sy][sx])
    return 0;
  queue<int> q;
  q.push(sx);
  q.push(sy);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int y = q.front();
    q.pop();
    mem[y][x] = 0;
    for (int i = 0; i < 8; i++) {
      int tx = x + dx[i];
      int ty = y + dy[i];
      if (tx >= 0 && tx < W && ty >= 0 && ty < H && mem[ty][tx]) {
        q.push(tx);
        q.push(ty);
      }
    }
  }
  return 1;
}

int main() {
loop:
  int ans = 0;
  for (int y = 0; y < 50; y++)
    for (int x = 0; x < 50; x++)
      mem[y][x] = 0;
  cin >> W >> H;
  if (!W)
    return 0;
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      cin >> mem[y][x];
  for (int y = 0; y < H; y++)
    for (int x = 0; x < W; x++)
      ans += bfs(x, y);
  cout << ans << endl;
  goto loop;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int x, y;
int o[52][52];
queue<int> q;
void tansaku(int j, int i) {
  if (j < 1 || j > x || i < 1 || i > y || o[i][j] == 0 || o[i][j] == 2) {
    return;
  }
  if (o[i][j] == 1) {
    if (q.empty())
      q.push(1);
    o[i][j] = 2;
  }

  tansaku(j + 1, i);
  tansaku(j - 1, i);
  tansaku(j, i + 1);
  tansaku(j, i - 1);
  tansaku(j + 1, i + 1);
  tansaku(j - 1, i - 1);
  tansaku(j + 1, i - 1);
  tansaku(j - 1, i + 1);
}
int main() {

  while (cin >> x >> y, x != 0 || y != 0) {
    int count = 0;
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> o[i][j];
      }
    }
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        tansaku(j, i);
        if (!q.empty()) {
          count += q.front();

          q.pop();
        }
      }
    }

    cout << count << endl;
  }
}

Error: WA

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int x, y;
int o[52][52];
queue<int> q;
void tansaku(int j, int i) {
  if (j < 1 || j > x || i < 1 || i > y || o[i][j] == 0 || o[i][j] == 2) {
    return;
  }
  if (o[i][j] == 1) {
    if (q.empty())
      q.push(1);
    o[i][j] = 2;
  }

  tansaku(j + 1, i);
  tansaku(j - 1, i);
  tansaku(j, i + 1);
  tansaku(j, i - 1);
  tansaku(j + 1, i + 1);
  tansaku(j - 1, i - 1);
  tansaku(j + 1, i - 1);
  tansaku(j - 1, i + 1);
}
int main() {

  while (cin >> x >> y, x != 0 || y != 0) {
    int count = 0;
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        cin >> o[i][j];
      }
    }
    for (int i = 1; i <= y; i++) {
      for (int j = 1; j <= x; j++) {
        tansaku(j, i);
        if (!q.empty()) {
          count += q.front();
        }
        q.pop();
      }
    }

    cout << count << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main(void) {
  int w, h;
  while (cin >> w >> h && w && h) {
    vector<vector<int>> data(h + 2, vector<int>(w + 2, 0));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> data[i][j];
      }
    }

    int nx, ny;
    int count = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (data[i][j] == 1) {
          nx = j;
          ny = i;
          data[i][j] = 0;
          queue<int> fx, fy;
          fx.push(nx);
          fy.push(ny);
          ++count;

          while (!fx.empty()) {
            nx = fx.front();
            ny = fy.front();
            fx.pop();
            fy.pop();
            for (int d = 0; d < 8; ++d) {
              if (data[ny + dy[d]][nx + dx[d]] == 1) {
                fx.push(nx + dx[d]);
                fy.push(ny + dy[d]);
                data[ny + dy[d]][nx + dx[d]] = 0;
              }
            }
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;
const int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};

int main(void) {
  int w, h;
  while (cin >> w >> h && w && h) {
    vector<vector<int>> data(w + 2, vector<int>(h + 2, 0));
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        cin >> data[i][j];
      }
    }

    int nx, ny;
    int count = 0;
    queue<int> fx, fy;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        if (data[i][j] == 1) {
          nx = j;
          ny = i;
          data[i][j] = 0;
          fx.push(nx);
          fy.push(ny);
          ++count;

          while (!fx.empty()) {
            nx = fx.front();
            ny = fy.front();
            fx.pop();
            fy.pop();
            for (int d = 0; d < 8; ++d) {
              if (data[ny + dy[d]][nx + dx[d]] == 1) {
                fx.push(nx + dx[d]);
                fy.push(ny + dy[d]);
                data[ny + dy[d]][nx + dx[d]] = 0;
              }
            }
          }
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int h, w;
void solve(int a[50][50], int x, int y) {
  int i, j;
  a[y][x] = 0;
  for (i = y - 1; i <= y + 1; i++) {
    for (j = x - 1; j <= x + 1; j++)
      if (0 <= i && i < h && 0 <= j && j < w && a[i][j])
        solve(a, j, i);
  }
  return;
}

int main(void) {
  int i, j, flag[50][50], count;
  scanf("%d%d", &w, &h);
  while (w != 0 || h != 0) {
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++)
        scanf("%d", &flag[i][j]);
    count = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (flag[i][j]) {
          count++;
          solve(flag, j, i);
        }
      }
    }
    printf("%d\\n", count);
    scanf("%d%d", &w, &h);
  }
  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>

void solve(int a[50][50], int x, int y) {
  int i, j;
  a[y][x] = 0;
  for (i = y - 1; i <= y + 1; i++) {
    for (j = x - 1; j <= x + 1; j++)
      if (a[i][j])
        solve(a, j, i);
  }
  return;
}

int main(void) {
  int w, h, i, j, flag[50][50], count;
  scanf("%d%d", &w, &h);
  while (w != 0 || h != 0) {
    for (i = 0; i < h; i++)
      for (j = 0; j < w; j++)
        scanf("%d", &flag[i][j]);
    count = 0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (flag[i][j]) {
          count++;
          solve(flag, j, i);
        }
      }
    }
    printf("%d\\n", count);
    scanf("%d%d", &w, &h);
  }
  return 0;
}