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

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[100][100], m[100][100];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
  while (scanf("%d%d", &w, &h)) {
    if (w == 0 && h == 0)
      break;
    memset(m, -1, sizeof(m));
    cnt = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        scanf("%d", &c[i][j]);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (m[i][j] == -1 && c[i][j] == 1) {
          cnt++;
          queue<P> q;
          q.push(P(i, j));
          m[i][j] = 0;
          while (!q.empty()) {
            P p = q.front();
            q.pop();
            for (int k = 0; k < 8; k++) {
              int ny = p.first + dy[k];
              int nx = p.second + dx[k];
              if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 &&
                  c[ny][nx] == 1) {
                m[ny][nx] = 0;
                q.push(P(ny, nx));
              }
            }
          }
        }
      }
    printf("%d\\n", cnt);
  }
}

Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1160&lang=jp
// ????????¶????????£???????????????????????????????????°???????????£?????????
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
int w, h, cnt;
int c[50][50], m[50][50];
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int main() {
  while (scanf("%d%d", &w, &h)) {
    if (w == 0 && h == 0)
      break;
    memset(m, -1, sizeof(m));
    cnt = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        scanf("%d", &c[i][j]);
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (m[i][j] == -1 && c[i][j] == 1) {
          cnt++;
          queue<P> q;
          q.push(P(i, j));
          while (!q.empty()) {
            P p = q.front();
            q.pop();
            m[p.first][p.second] = 0;
            for (int k = 0; k < 8; k++) {
              int ny = p.first + dy[k];
              int nx = p.second + dx[k];
              if (nx >= 0 && nx < w && ny >= 0 && ny < h && m[ny][nx] == -1 &&
                  c[ny][nx] == 1) {
                q.push(P(ny, nx));
              }
            }
          }
        }
      }
    printf("%d\\n", cnt);
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
void dfs(int, int);
int box[52][52];
int main() {
  int w, h, i, I, a;
  while (1) {
    a = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (i = 0; i <= h + 1; i++) {
      for (I = 0; I <= w + 1; I++) {
        box[i][I] == 0;
      }
    }
    for (i = 1; i <= h; i++) {
      for (I = 1; I <= w; I++) {
        cin >> box[i][I];
      }
    }
    for (i = 1; i <= h; i++) {
      for (I = 1; I <= w; I++) {
        if (box[i][I] == 1) {
          dfs(i, I);
          a++;
        }
      }
    }
    cout << a << endl;
  }
  return 0;
}
void dfs(int i, int I) {
  if (box[i][I] == 0)
    return;
  box[i][I] = 0;
  dfs(i, I + 1);
  dfs(i + 1, I + 1);
  dfs(i + 1, I);
  dfs(i + 1, I - 1);
  dfs(i, I - 1);
  dfs(i - 1, I - 1);
  dfs(i - 1, I);
  dfs(i - 1, I + 1);
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
void kazoeruyo(int p, int P, int box[50][50]) {
  box[p][P] = 0;
  if (box[p][P + 1] == 1) {
    box[p][P + 1] = 0;
    P = P + 1;
    kazoeruyo(p, P, box);
  }
  if (box[p][P - 1] == 1) {
    box[p][P - 1] = 0;
    P = P - 1;
    kazoeruyo(p, P, box);
  }
  if (box[p + 1][P] == 1) {
    box[p + 1][P] = 0;
    p = p + 1;
    kazoeruyo(p, P, box);
  }
  if (box[p - 1][P] == 1) {
    box[p - 1][P] = 0;
    p = p - 1;
    kazoeruyo(p, P, box);
  }
  if (box[p - 1][P - 1] == 1) {
    box[p - 1][P - 1] = 0;
    p = p - 1;
    P = P - 1;
    kazoeruyo(p, P, box);
  }
  if (box[p - 1][P + 1] == 1) {
    box[p - 1][P + 1] = 0;
    p = p - 1;
    P = P + 1;
    kazoeruyo(p, P, box);
  }
  if (box[p + 1][P + 1] == 1) {
    box[p + 1][P + 1] = 0;
    p = p + 1;
    P = P + 1;
    kazoeruyo(p, P, box);
  }
  if (box[p + 1][P - 1] == 1) {
    box[p + 1][P - 1] = 0;
    p = p + 1;
    P = P - 1;
    kazoeruyo(p, P, box);
  }
}
int main() {
  int w, h, i, I, p, P, o, t, T, y, Y;
  int box[50][50];
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (i = 0; i < h; i++) {
      for (I = 0; I < w; I++) {
        cin >> box[i][I];
      }
    }
    o = 0;
    for (i = 0; i < h; i++) {
      for (I = 0; I < w; I++) {
        if (box[i][I] == 1) {
          p = i;
          P = I;
          kazoeruyo(p, P, box);
          box[p][P] = 1;
        }
      }
    }
    for (i = 0; i < h; i++) {
      for (I = 0; I < w; I++) {
        if (box[i][I] == 1)
          o++;
      }
    }
    cout << o << endl;
    o = 0;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

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

void check(int x, int y) {
  int mx, my;

  for (int i = 0; i < 8; i++) { // üÍWûüð²×é
    mx = x + dx[i];
    my = y + dy[i];

    // tB[hOÉoÄ¢È¯êÎ²×é(0ÈãE³Èà)
    if (mx >= 0 && mx < w && my >= 0 && my < h) {
      if (map[my][mx] == 1) { // ¾Á½ç
        map[my][mx] =
            2; // ñdÉ²×È¢½ßÉÊÌlðüêé
        check(mx, my);
      }
    }
  }
}

int main() {
  int count; // ÌðJEg·éÏ

  while (1) {
    // ÏÌú»
    count = 0;

    // E³ÌüÍ
    cin >> w >> h;
    if (w == 0 && h == 0)
      break; // 0 0ªüÍ³ê½çI¹

    // tB[hª©C©ÌîñðüÍ
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> map[y][x];
      }
    }

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (map[y][x] ==
            1) { // ¾Á½çÄAÖðÄÑo·
          map[y][x] = 2;
          count++;
          check(x, y);
        }
      }
    }

    // ¦ÌoÍ
    cout << count << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

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

int map[51][51];
int w, h;

// void printmap() {
//	for (int i = 0 ; i < h; i++) {
//		for (int j = 0; j < w; j++)
//			cout << map[i][j] << " ";
//		cout << endl;
//	}
// }

void oneIsland(int x, int y) {
  for (int i = 0; i < 8; i++) {
    int nextX = x + dx[i];
    int nextY = y + dy[i];
    if (x < 0 || w <= x)
      continue;
    if (y < 0 || w <= y)
      continue;
    if (map[nextY][nextX] == 0)
      continue;
    map[nextY][nextX] = 0;
    oneIsland(nextX, nextY);
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> map[i][j];
      }
    }

    int count = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (map[i][j] == 1) {
          map[i][j] = 0;
          oneIsland(j, i);
          // printmap();
          count++;
        }
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

// 41
#include <iostream>

using namespace std;

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

void ff(int y, int x) {
  imap[y][x] = false;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int ny = y + i, nx = x + j;
      if (0 <= ny && ny < h && 0 <= nx && nx < w && imap[ny][nx]) {
        ff(ny, nx);
      }
    }
  }
}

int main() {
  for (; cin >> w >> h, w | h;) {
    fill_n(&imap[0][0], 50 * 50, false);
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> imap[y][x];
      }
    }
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (imap[y][x]) {
          ans++;
          ff(y, x);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// 41
#include <iostream>

using namespace std;

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

void ff(int y, int x) {
  imap[y][x] = false;
  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      if (imap[y + i][x + j]) {
        ff(y + i, x + j);
      }
    }
  }
}

int main() {
  for (; cin >> w >> h, w | h;) {
    fill_n(&imap[0][0], 50 * 50, false);
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        cin >> imap[y][x];
      }
    }
    int ans = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        if (imap[y][x]) {
          ans++;
          ff(y, x);
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int a[50][50] = {};
int w, h;

void island(int x, int y) {
  a[x][y] = 0;
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; j++) {
      if (y + j < 0 || y + j > h - 1 || x + i > w - 1 || x + i < 0) {
        continue;
      }
      if (a[x + i][y + j] == 1) {
        island(x + i, y + j);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, !(w == 0 && h == 0)) {
    REP(i, h) {
      REP(j, w) { cin >> a[j][i]; }
    }
    int ans(0);
    REP(i, h) {
      REP(j, w) {
        if (a[j][i] == 1) {
          island(j, i);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define REP(i, n) for (int i = 0; i < n; i++)
typedef long long ll;
using namespace std;
typedef vector<ll> vl;
typedef pair<ll, ll> pll;
typedef vector<pll> vpll;
typedef vector<string> vs;

int a[50][50] = {};
int w, h;

void island(int x, int y) {
  a[x][y] = 0;
  for (int i = -1; i < 2; i++) {
    for (int j = -1; j < 2; y++) {
      if (y + j < 0 || y + j > h - 1 || x + i > w - 1 || x + i < 0) {
        continue;
      }
      if (a[x + i][y + j] == 1) {
        island(x + i, y + j);
      }
    }
  }
}

int main() {
  while (cin >> w >> h, !(w == 0 && h == 0)) {
    REP(i, h) {
      REP(j, w) { cin >> a[j][i]; }
    }
    int ans(0);
    REP(i, h) {
      REP(j, w) {
        if (a[j][i] == 1) {
          island(j, i);
          ans++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <utility>

using namespace std;
using Point = pair<int, int>;

int map[52][52];

void BFS(const Point &p0) {
  queue<Point> q;
  q.push(p0);
  map[p0.first][p0.second] = 0;
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.first, x = p.second;

    for (int i = -1; i <= 1; ++i) {
      for (int j = -1; j <= 1; ++j) {
        if (i == 0 && j == 0)
          continue;
        if (map[i + y][j + x]) {
          q.push(Point(i + y, j + x));
          map[i + y][j + x] = 0;
        }
      }
    }
  }
}

int main() {
  int w, h;
  while (cin >> w >> h && w > 0 && h > 0) {

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

    int num = 0;
    for (int i = 1; i < h + 1; ++i) {
      for (int j = 1; j < w + 1; ++j) {
        if (map[i][j]) {
          BFS(Point(i, j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>

using namespace std;

using Point = pair<int, int>;

int map[52][52];

void BFS(const Point &p0) {
  queue<Point> q;
  q.push(p0);
  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int y = p.first, x = p.second;
    map[y][x] = 0;

    for (int i = -1; i <= 1; ++i) {
      for (int j = -1; j <= 1; ++j) {
        if (map[i + y][j + x]) {
          q.push(Point(i + y, j + x));
        }
      }
    }
  }
}

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

    int num = 0;
    for (int i = 1; i < h + 1; ++i) {
      for (int j = 1; j < w + 1; ++j) {
        if (map[i][j]) {
          BFS(Point(i, j));
          num++;
        }
      }
    }
    cout << num << endl;
  }

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

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

int map[50][50];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy) {
  queue<pxy> q;
  q.push(pxy(sx, sy));
  while (!q.empty()) {
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (map[x][y]) {
      map[x][y] = 0;
      for (int i = 0; i < 8; i++) {
        int next_x = x + dx[i], next_y = y + dy[i];
        if (map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h &&
            next_y >= 0) {
          q.push(pxy(next_x, next_y));
        }
      }
    }
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    } else {
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          cin >> map[j][i];
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          if (map[j][i]) {
            ans++;
            bfs(j, i);
          }
      cout << ans << endl;
      ans = 0;
    }
  }
}

Error: 0

Fixed submission:

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

int map[20][20];

typedef pair<int, int> pxy;

int h, w, ans = 0;

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

void bfs(int sx, int sy) {
  queue<pxy> q;
  q.push(pxy(sx, sy));
  while (!q.empty()) {
    int x, y;
    x = q.front().first;
    y = q.front().second;
    q.pop();
    if (map[x][y]) {
      map[x][y] = 0;
      for (int i = 0; i < 8; i++) {
        int next_x = x + dx[i], next_y = y + dy[i];
        if (map[next_x][next_y] && next_x < w && next_x >= 0 && next_y < h &&
            next_y >= 0) {
          q.push(pxy(next_x, next_y));
        }
      }
    }
  }
}

int main() {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    } else {
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          cin >> map[j][i];
      for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
          if (map[j][i]) {
            ans++;
            bfs(j, i);
          }
      cout << ans << endl;
      ans = 0;
    }
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define _overload(_1, _2, _3, name, ...) name
#define _rep(i, n) _range(i, 0, n)
#define _range(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rep(...) _overload(__VA_ARGS__, _range, _rep, )(__VA_ARGS__)
#define _all(arg) begin(arg), end(arg)

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

using namespace std;
using vi = vector<int>;
using vii = vector<vi>;

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

int w, h;

int dfs(vii &m, int pos_h, int pos_w) {
  int res = 0;
  if (0 <= pos_h && pos_h < h && 0 <= pos_w && pos_w < w &&
      m[pos_h][pos_w] == 1) {
    m[pos_h][pos_w] = 0;
    res++;
  } else {
    return 0;
  }
  rep(i, 8) { res += dfs(m, pos_h + dx[i], pos_w + dy[i]); }
  return (res) ? 1 : 0;
}

int main(void) {
  while (cin >> w >> h, w && h) {
    vii m(h, vi(w));
    rep(i, h) rep(j, w) { cin >> m[i][j]; }
    int res = 0;
    rep(i, h) rep(j, w) res += dfs(m, i, j);
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <utility>

using namespace std;

bool check(bool[50][50], pair<int, int>);
void rmv(bool[50][50], int, int);

int main() {
  for (int w, h; cin >> w >> h, w || h;) {
    bool fld[50][50] = {0};
    int n;

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

    for (n = 0; check(fld, make_pair(w, h)); n++)
      ;
    cout << n << endl;
  }
  return 0;
}

bool check(bool fld[50][50], pair<int, int> wide) {
  for (int i = 0; i < wide.second; i++)
    for (int j = 0; j < wide.first; j++)
      if (fld[i][j]) {
        rmv(fld, i, j);
        return true;
      }
  return false;
}

void rmv(bool fld[50][50], int y, int x) {
  if (x >= 0 && y >= 0)
    if (fld[y][x]) {
      fld[y][x] = false;
      for (int i = -1; i <= 1; i++)
        for (int j = -1; j <= 1; j++)
          rmv(fld, y + i, x + j);
    }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define llong long long
int w, h;
int sum;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int H, int W) {
  if (area[H][W] == 0)
    return;
  area[H][W] = 0;
  for (int i = 0; i < 8; i++) {
    int sh = H + nh[i];
    int sw = W + nw[i];
    if (sh < 0 || sw < 0 || sh >= h || sw >= w)
      continue;
    solve(sh, sw);
  }

  return;
}

int main() {

  while (cin >> w >> h, w) {
    sum = 0;
    fill_n(*area, 50 * 50, 0);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> area[i][j];
      }
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (area[i][j] == 1) {
          sum++;
          solve(i, j);
        }
      }
    }

    cout << sum << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define llong long long
int w, h;
int sum = 0;
int area[50][50];
int nw[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int nh[8] = {1, 1, 0, -1, -1, -1, 0, 1};

void solve(int h, int w) {
  if (area[h][w] == 0)
    return;
  area[h][w] == 0;
  for (int i = 0; i < 8; i++) {
    int sh = h + nh[i];
    int sw = w + nw[i];
    solve(sh, sw);
  }
  return;
}

int main() {
  fill_n(*area, 50 * 50, 0);
  cin >> w >> h;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> area[i][j];
    }
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (area[i][j] == 1) {
        sum++;
        solve(i, j);
      }
    }
  }

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

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

#define int long long

int w, h;
int field[51][51];

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

void dfs(int x, int y) {
  field[x][y] = 0;
  for (int i = 0; i < 8; ++i) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < h && ny >= 0 && ny < w && field[nx][ny] == 1)
      dfs(nx, ny);
  }
}

signed main() {
  while (cin >> w >> h, w) {
    for (int i = 0; i < h; ++i)
      for (int j = 0; j < w; ++j)
        field[i][j] = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> field[i][j];
      }
    }

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

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define int long long

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

signed main() {
  int w, h;
  while (cin >> w >> h, w) {
    int field[h + 1][w + 1];
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        int c;
        cin >> c;
        field[i][j] = c;
      }
    }

    int ans = 0;
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        if (field[i][j] == 1) {
          ans++;
          queue<pair<int, int>> q;
          q.push(make_pair(i, j));
          while (!q.empty()) {
            pair<int, int> p = q.front();
            q.pop();
            int x = p.first;
            int y = p.second;
            field[x][y] = 0;
            for (int k = 0; k < 8; ++k) {
              int nx = x + dx[k];
              int ny = y + dy[k];
              if (nx >= 0 && nx < h && ny >= 0 && ny < w &&
                  field[nx][ny] == 1) {
                q.push(make_pair(nx, ny));
              }
            }
          }
        }
      }
    }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double eps = 1e-10;

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

int main() {

  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> table(h, vector<int>(w));
    int res = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++)
        cin >> table[y][x];
    }

    queue<pair<int, int>> q;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (table[i][j] == 0)
          continue;

        res++;
        q.push(make_pair(i, j));

        while (!q.empty()) {
          pair<int, int> p = q.front();
          q.pop();
          int y = p.first, x = p.second;

          //					cout<<y<<" "<<x<<endl;

          if (table[y][x] == 0)
            continue;

          table[y][x] = 0;
          for (int k = 0; k < 8; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < w && 0 <= ny && ny < h && table[ny][nx] == 1)
              q.push(make_pair(ny, nx));
          }
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

const double eps = 1e-10;

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

int main() {

  int w, h;
  while (cin >> w >> h, w) {
    vector<vector<int>> table(h, vector<int>(w));
    int res = 0;
    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++)
        cin >> table[y][x];
    }

    queue<pair<int, int>> q;

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {

        if (table[i][j] == 0)
          continue;

        res++;
        q.push(make_pair(i, j));

        while (!q.empty()) {
          pair<int, int> p = q.front();
          q.pop();
          int y = p.first, x = p.second;

          table[y][x] = 0;
          for (int k = 0; k < 8; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if (0 <= nx && nx < w && 0 <= ny && ny < h && table[ny][nx] == 1)
              q.push(make_pair(ny, nx));
          }
        }
      }
    }
    cout << res << endl;
  }

  return 0;
}