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

#include <cstdio>
using namespace std;
void func(int, int);
int a, b;
int sima[51][51];
int cnt;
int main(void) {
  int i, j, x, y;
  scanf("%d", &a);
  scanf("%d", &b);
  while (!(b == 0 && a == 0)) {

    for (i = 0; i < b; i++) {
      for (j = 0; j < a; j++) {
        scanf("%d", &sima[i][j]);
      }
    }
    cnt = 0;
    for (i = 0; i < b; i++) {
      for (j = 0; j < a; j++) {
        if (sima[i][j] == 1) {
          func(i, j);
          cnt++;
        }
      }
    }
    printf("%d\\n", cnt);
    scanf("%d", &a);
    scanf("%d", &b);
  }
  return 0;
}
void func(int y, int x) {
  if (y - 1 >= 0) {
    if (sima[y - 1][x] == 1) {
      sima[y - 1][x] = 0;
      func(y - 1, x);
    }
  }
  if (x - 1 >= 0) {
    if (sima[y][x - 1] == 1) {
      sima[y][x - 1] = 0;
      func(y, x - 1);
    }
  }
  if (y + 1 <= b) {
    if (sima[y + 1][x] == 1) {
      sima[y + 1][x] = 0;
      func(y + 1, x);
    }
  }
  if (x + 1 <= a) {
    if (sima[y][x + 1] == 1) {
      sima[y][x + 1] = 0;
      func(y, x + 1);
    }
  }
  if (y - 1 >= 0 && x + 1 <= a) {
    if (sima[y - 1][x + 1] == 1) {
      sima[y - 1][x + 1] = 0;
      func(y - 1, x + 1);
    }
  }
  if (y - 1 >= 0 && x - 1 >= 0) {
    if (sima[y - 1][x - 1] == 1) {
      sima[y - 1][x - 1] = 0;
      func(y - 1, x - 1);
    }
  }
  if (y + 1 <= b && x + 1 <= a) {
    if (sima[y + 1][x + 1] == 1) {
      sima[y + 1][x + 1] = 0;
      func(y + 1, x + 1);
    }
  }
  if (y + 1 <= b && x - 1 >= 0) {
    if (sima[y + 1][x - 1] == 1) {
      sima[y + 1][x - 1] = 0;
      func(y + 1, x - 1);
    }
  }
}


Error: WA

Fixed submission:

#include <cstdio>
using namespace std;
void func(int, int);
int a, b;
int sima[51][51];
int cnt;
int main(void) {
  int i, j;
  scanf("%d", &a);
  scanf("%d", &b);
  for (i = 0; i < a; i++) {
    for (j = 0; j < b; j++) {
      scanf("%d", &sima[i][j]);
    }
  }
  for (i = 0; i < a; i++) {
    for (j = 0; j < b; j++) {
      if (sima[i][j] == 1) {
        func(i, j);
        cnt++;
      }
    }
  }
  printf("%d\\n", cnt);
  return 0;
}
void func(int y, int x) {
  if (y - 1 >= 0) {
    if (sima[y - 1][x] == 1) {
      func(y - 1, x);
    }
  }
  if (x - 1 >= 0) {
    if (sima[y][x - 1] == 1) {
      func(y, x - 1);
    }
  }
  if (y + 1 <= a) {
    if (sima[y + 1][x] == 1) {
      func(y + 1, x);
    }
  }
  if (x + 1 <= b) {
    if (sima[y][x + 1] == 1) {
      func(y, x + 1);
    }
  }
  if (y - 1 >= 0 && x + 1 <= b) {
    if (sima[y - 1][x + 1] == 1) {
      func(y - 1, x + 1);
    }
  }
  if (y - 1 >= 0 && x - 1 >= 0) {
    if (sima[y - 1][x - 1] == 1) {
      func(y - 1, x - 1);
    }
  }
  if (y + 1 <= a && x + 1 <= b) {
    if (sima[y + 1][x + 1]) {
      func(y + 1, x + 1);
    }
  }
  if (y + 1 <= a && x - 1 >= 0) {
    if (sima[y + 1][x - 1]) {
      func(y + 1, x - 1);
    }
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

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

int main() {
  int i, j, ans;
  cin >> w >> h;
  while (1) {
    ans = 0;
    if (w + h == 0)
      break;
    // for(i=0;i<50;i++)for(j=0;j<50;j++)c[i][j]=0;
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++)
        cin >> c[i][j];
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++)
        if (c[i][j]) {
          dfs(i, j);
          ans++;
        }
    }
    cout << ans << endl;
    cin >> w >> h;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int c[50][50], w, h;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dy[] = {-1, 0, 1, -1, 1, -1, 0, 1};
void dfs(int y, int x) {
  if (c[y][x]) {
    c[y][x] = 0;
    int i;
    for (i = 0; i < 8; i++) {
      if (y + dy[i] >= 0 || y + dy[i] < h || x + dx[i] >= 0 || x + dx[i] < w)
        dfs(y + dy[i], x + dx[i]);
    }
  }
  return;
}

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

#include <iostream>

#define W_MAX 52
#define H_MAX 52

void dfs(int f[H_MAX][W_MAX], int y, int x);

using namespace std;

int main(void) {
  int w, h;

  int i1, i2;
  int count;

  int f[H_MAX][W_MAX];

  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    for (i1 = 0; i1 < H_MAX; i1++) {
      for (i2 = 0; i2 < W_MAX; i2++) {
        f[i1][i2] = 0;
      }
    }

    for (i1 = 1; i1 <= h; i1++) {
      for (i2 = 1; i2 <= w; i2++) {
        cin >> f[i1][i2];
      }
    }

    count = 0;
    for (i1 = 1; i1 <= h; i1++) {
      for (i2 = 1; i2 <= w; i2++) {
        if (f[i1][i2] == 1) {
          count++;
          dfs(f, i1, i2);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

void dfs(int f[H_MAX][W_MAX], int y, int x) {
  int i1, i2;

  f[y][x] = 0;
  for (i1 = -1; i1 < 2; i1++) {
    for (i2 = -1; i2 < 2; i2++) {

      if (f[y + i1][x + i2] == 1)
        dfs(f, y + i1, x + i2);
    }
  }
  return;
}

Error: -11

Fixed submission:

#include <iostream>

#define W_MAX 55
#define H_MAX 55

int f[H_MAX][W_MAX];

void dfs(int y, int x);

using namespace std;

int main(void) {
  int w;
  int h;
  int i1, i2;
  int count;
  int a;

  while (1) {
    cin >> w;
    cin >> h;
    if (w == 0 && h == 0)
      break;

    // cout << "a";

    for (i1 = 0; i1 < H_MAX; i1++) {
      for (i2 = 0; i2 < W_MAX; i1++) {
        f[i1][i2] = 0;
      }
    }

    for (i1 = 1; i1 <= h; i1++) {
      for (i2 = 1; i2 <= w; i2++) {
        cin >> a;
        // f[i1][i2] = a;
      }
    }

    count = 0;
    for (i1 = 1; i1 <= h; i1++) {
      for (i2 = 1; i2 <= w; i2++) {
        if (f[i1][i2] == 1) {
          count++;
          dfs(i1, i2);
        }
      }
    }

    cout << count << endl;
  }
  return 0;
}

void dfs(int y, int x) {
  int i1, i2;

  f[y][x] = 0;
  for (i1 = -1; i1 < 2; i1++) {
    for (i2 = -1; i2 < 2; i2++) {

      if (f[y + i1][x + i2] == 1)
        dfs(y + i1, x + i2);
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define pb(in, tmp) in.push_back(tmp)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) loop(i, 0, b)
#define all(in) in.begin(), in.end()
using namespace std;
void isl(int in[50][50], int x, int y) {
  in[x][y] = 0;
  loop(i, -1, 2) {
    loop(j, -1, 2) {
      if (x - i >= 0 && x - i < 50 && y - j >= 0 && y - j < 50 &&
          in[x - i][y - j])
        isl(in, x - i, y - j);
    }
  }
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int in[50][50] = {0};
    int co = 0;
    rep(i, m) rep(j, n) cin >> in[i][j];

    rep(i, m) {
      rep(j, n) {
        if (in[i][j]) {
          co++;
          isl(in, i, j);
        }
      }
    }
    cout << co << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#define pb(in, tmp) in.push_back(tmp)
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, b) loop(i, 0, b)
#define all(in) in.begin(), in.end()
using namespace std;
void isl(int in[50][50], int x, int y) {
  in[x][y] = 0;
  loop(i, -1, 2) {
    loop(j, -1, 2) {
      if (x - i >= 0 && y - j >= 0 && in[x - i][y - j])
        isl(in, x - i, y - j);
    }
  }
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    int in[50][50] = {0};
    int co = 0;
    rep(i, m) rep(j, n) cin >> in[i][j];

    rep(i, m) {
      rep(j, n) {
        if (in[i][j]) {
          co++;
          isl(in, i, j);
        }
      }
    }
    cout << co << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  bool map[52][52];
  int w, h;

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

    int land = 0;

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

    int visited = 0, cnt = 0;
    queue<int> cur;
    int d[8] = {-w - 3, -w - 2, -w - 1, -1, 1, w + 1, w + 2, w + 3};

    while (visited < land) {
      cnt++;
      for (int i = 0; i < (w + 2) * (h + 2); i++) {
        if (map[i / (w + 2)][i % (w + 2)]) {
          map[i / (w + 2)][i % (w + 2)] = false;
          visited++;
          cur.push(i);
          break;
        }
      }
      while (!cur.empty()) {
        for (int i = 0; i < 8; i++) {
          if (map[(cur.front() + d[i]) / (w + 2)]
                 [(cur.front() + d[i]) % (w + 2)]) {
            map[(cur.front() + d[i]) / (w + 2)]
               [(cur.front() + d[i]) % (w + 2)] = false;
            visited++;
            cur.push(cur.front() + d[i]);
          }
        }
        cur.pop();
      }
    }

    cout << cnt << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  bool map[52][52];
  int w, h;

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

    int land = 0;

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

    int visited = 0, cnt = 0;
    queue<int> cur;
    int d[8] = {-w - 3, -w - 2, -w - 1, -1, 1, w + 1, w + 2, w + 3};

    while (visited < land) {
      cnt++;
      for (int i = 0; i < (w + 2) * (h + 2); i++) {
        if (map[i / (w + 2)][i % (w + 2)]) {
          map[i / (w + 2)][i % (w + 2)] = false;
          visited++;
          cur.push(i);
          break;
        }
      }
      while (!cur.empty()) {
        for (int i = 0; i < 8; i++) {
          if (map[(cur.front() + d[i]) / (w + 2)]
                 [(cur.front() + d[i]) % (w + 2)]) {
            map[(cur.front() + d[i]) / (w + 2)]
               [(cur.front() + d[i]) % (w + 2)] = false;
            visited++;
            cur.push(cur.front() + d[i]);
          }
        }
        cur.pop();
      }
    }

    cout << cnt << endl;
  }

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

#include <iostream>
#include <vector>
using namespace std;
int needx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int needy[] = {0, 1, 0, -1, 1, -1, 1, -1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x, int y, vector<vector<bool>> &cango) {
  if (cango[x][y]) {
    cango[x][y] = false;
    for (int i = 0; i < 8; i++) {
      int nextx = x + needx[i];
      int nexty = y + needy[i];
      if (cango[nextx][nexty])
        bool flag = land(nextx, nexty, cango);
    }
    return true;
  } else {
    return false;
  }
}
int main(void) {
  int input;
  int w, h;
  while (1) {
    cin >> w >> h;
    if (h == 0)
      break;
    vector<vector<bool>> cango(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          cango[i][j] = true;
        }
      }
    }

    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (land(i, j, cango)) {
          t++;
        }
      }
    }
    cout << t << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int needx[] = {-1, 0, 1, 0, 1, 1, -1, -1};
int needy[] = {0, 1, 0, -1, 1, -1, 1, -1};
//??¶?????¨???false????????????true???????????¢??°//
bool land(int x, int y, vector<vector<bool>> &cango) {
  if (cango[x][y]) {
    cango[x][y] = false;
    for (int i = 0; i < 8; i++) {
      int nextx = x + needx[i];
      int nexty = y + needy[i];
      if (cango[nextx][nexty])
        bool flag = land(nextx, nexty, cango);
    }
    return true;
  } else {
    return false;
  }
}
int main(void) {
  int input;
  int w, h;
  while (1) {
    cin >> w >> h;
    if (h == 0)
      break;
    vector<vector<bool>> cango(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> input;
        if (input == 1) {
          cango[i][j] = true;
        }
      }
    }

    int t = 0;
    for (int i = 1; i <= w; i++) {
      for (int j = 1; j <= h; j++) {
        if (land(i, j, cango)) {
          t++;
        }
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

void proc(int *maps, int p, int w, int h) {
  if (maps[p] == 1)
    maps[p] = 2;
  else
    return;
  int x = p % w;
  int y = p / w;
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      int nx = x + j;
      int ny = y + i;
      if (nx >= 0 && ny >= 0 && nx < w && ny < h) {
        proc(maps, nx + ny * w, w, h);
      }
    }
  }
}

int main(void) {
  int w, h;
  while (cin >> w >> h, w && h) {
    int chizu[2500];
    for (int i = 0; i < h; ++i) {
      for (int j = 0, a; j < w; ++j) {
        cin >> a;
        chizu[i * w + j] = a;
      }
    }
    int count = 0;
    for (int i = 0; i < w * h; ++i) {
      if (chizu[i] == 1) {
        proc(chizu, i, w, h);
        ++count;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

void proc(vector<int> &maps, int p, int w, int h) {
  if (maps[p] == 1)
    maps[p] = 2;
  else
    return;
  int x = p % w;
  int y = p / w;
  for (int i = -1; i <= 1; ++i) {
    for (int j = -1; j <= 1; ++j) {
      int nx = x + j;
      int ny = y + i;
      if (nx >= 0 && ny >= 0 && nx < w && ny < h) {
        proc(maps, nx + ny * w, w, h);
      }
    }
  }
}

int main(void) {
  int w, h;
  while (cin >> w >> h, w && h) {
    vector<int> chizu(w * h);
    for (int i = 0; i < h; ++i) {
      for (int j = 0, a; j < w; ++j) {
        cin >> a;
        chizu[i * h + j] = a;
      }
    }
    int count = 0;
    for (int i = 0; i < w * h; ++i) {
      if (chizu[i] == 1) {
        proc(chizu, i, w, h);
        ++count;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <stack>
using namespace std;

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

int main() {
  int w, h, ans, x, y;
  int c[52][52], f[52][52];
  stack<pair<int, int>> s;
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h + 2; ++i)
      for (int j = 0; j < w + 2; ++j) {
        c[i][j] = 0;
        f[i][j] = 0;
      }
    ans = 0;

    for (int i = 1; i <= h; ++i)
      for (int j = 1; j <= w; ++j) {
        cin >> c[i][j];
      }
    for (int i = 1; i <= h; ++i)
      for (int j = 1; j <= w; ++j) {
        if (c[i][j] && !f[i][j]) {
          ans++;
          s.push(pair<int, int>(i, j));
          while (!s.empty()) {
            y = s.top().first;
            x = s.top().second;
            s.pop();
            f[y][x] = 1;
            for (int k = 0; k < 8; ++k) {
              if (c[y + dy[k]][x + dx[k]] && !f[y + dy[k]][x + dx[k]]) {
                s.push(pair<int, int>(y + dy[k], x + dx[k]));
              }
            }
          }
        }
      }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

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

int main() {
  int w, h, ans, x, y;
  int c[52][52], f[52][52];
  queue<pair<int, int>> q;
  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    for (int i = 0; i < h + 2; ++i)
      for (int j = 0; j < w + 2; ++j) {
        c[i][j] = 0;
        f[i][j] = 0;
      }
    ans = 0;

    for (int i = 1; i <= h; ++i)
      for (int j = 1; j <= w; ++j) {
        cin >> c[i][j];
      }
    for (int i = 1; i <= h; ++i)
      for (int j = 1; j <= w; ++j) {
        if (c[i][j] && !f[i][j]) {
          ans++;
          q.push(pair<int, int>(i, j));
          while (!q.empty()) {
            y = q.front().first;
            x = q.front().second;
            q.pop();
            f[y][x] = 1;
            for (int k = 0; k < 8; ++k) {
              if (c[y + dy[k]][x + dx[k]] && !f[y + dy[k]][x + dx[k]]) {
                q.push(pair<int, int>(y + dy[k], x + dx[k]));
              }
            }
          }
        }
      }
    cout << ans << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int m[51][51];
bool v[51][51];
int h, w;
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
void dfs(int x, int y) {
  if (v[y][x])
    return;
  v[y][x] = 1;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && !v[ny][nx] && m[ny][nx]) {
      dfs(nx, ny);
    }
  }
}
int main() {
  while (cin >> w >> h, h) {
    int c = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        cin >> m[i][j];
        v[i][j] = 0;
      }
    }
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (!v[i][j] && m[i][j]) {
          dfs(j, i);
          c++;
        }
      }
    }
    cout << c << endl;
  }
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;

int m[51][51];
bool v[51][51];
int h, w, c = 0;
int dx[8] = {-1, -1, 0, 1, 1, 1, 0, -1};
int dy[8] = {0, -1, -1, -1, 0, 1, 1, 1};
void dfs(int x, int y) {
  if (v[y][x])
    return;
  for (int i = 0; i < 8; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (nx >= 0 && nx < w && ny >= 0 && ny < h && !v[y][x] && m[ny][nx]) {
      dfs(nx, ny);
    }
  }
}
int main() {
  cin >> h >> w;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> m[i][j];
      v[i][j] = 0;
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (!v[i][j] && m[i][j]) {
        dfs(i, j);
        c++;
      }
    }
  }
  cout << c << endl;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

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

void dfs(int a, int b) {
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      int A = a + i - 1, B = b + j - 1;
      if (0 <= A && A < w && 0 <= B && B < h && c[A][B]) {
        c[A][B] = 0;
        dfs(A, B);
      }
    }
  }
}

int main() {
  while (scanf("%d%d", &w, &h), w || h) {
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &c[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        if (c[i][j]) {
          c[i][j] = 0;
          ans++;
          dfs(i, j);
        }
      }
    }
    printf("%d\\n", ans);
  }
}

Error: -11

Fixed submission:

#include <cstdio>
#include <map>
#include <queue>
using namespace std;
typedef pair<int, int> P;

int w, h;
int c[50][50];
queue<P> que;

void dfs() {
  P p = que.front();
  que.pop();
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++) {
      if (c[p.first + i - 1][p.second + j - 1]) {
        c[p.first + i - 1][p.second + j - 1] = false;
        que.push(P(p.first + i - 1, p.second + j - 1));
        dfs();
      }
    }
  }
}

int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w || h) {
    int ans = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &c[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      for (int j = 0; j < h; j++) {
        if (c[i][j]) {
          c[i][j] = false;
          ans++;
          que.push(P(i, j));
          dfs();
        }
      }
    }
    printf("%d\\n", ans);
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, n) for (int i = (int)p; i < (int)n; i++)
#define repp(i, p, n) for (int i = (int)p; i >= (int)n; i--)
#define Sort(x) sort(x.begin(), x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;

vector<vector<int>> v;
int w, h;

void dfs(int y, int x) {
  if (v[y][x] == 1)
    v[y][x] = 0;

  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i, ny = y + j;

      if (!(nx < 0 || ny < 0 || nx >= w || ny >= h) && v[ny][nx] == 1)
        dfs(ny, nx);
    }
  }

  return;
}

int main() {
  while (1) {
    int ans = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    v.resize(h, vector<int>(w));
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> v[i][j]; }
    }

    rep(i, 0, h) {
      rep(j, 0, w) {
        if (v[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;
    v.clear();
  }

  return 0;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS

#include <algorithm>
#include <cmath>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define rep(i, p, n) for (int i = (int)p; i < (int)n; i++)
#define repp(i, p, n) for (int i = (int)p; i >= (int)n; i--)
#define Sort(x) sort(x.begin(), x.end())
#define PSort(x) sort(begin(x), end(x), pairCompare)
#define ll long long
#define INF 2000000000
#define pb push_back
#define mp make_pair
#define PI 3.14159265358979
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
using namespace std;

vector<vector<int>> v;
int w, h;

void dfs(int y, int x) {
  if (v[y][x] == 1)
    v[y][x] = 0;

  for (int i = -1; i <= 1; i++) {
    for (int j = -1; j <= 1; j++) {
      int nx = x + i, ny = y + j;

      if (!(nx < 0 || ny < 0 || nx >= w || ny >= h) && v[ny][nx] == 1)
        dfs(ny, nx);
    }
  }

  return;
}

int main() {
  while (1) {
    int ans = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    v.resize(h, vector<int>(w));
    rep(i, 0, h) {
      rep(j, 0, w) { cin >> v[i][j]; }
    }

    rep(i, 0, h) {
      rep(j, 0, w) {
        if (v[i][j] == 1) {
          ans++;
          dfs(i, j);
        }
      }
    }
    cout << ans << endl;
  }

  return 0;
}