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

#include <stdio.h>
int main() {
  int t[50], n, s, storn, i, j;
  while (1) {
    scanf("%d %d", &n, &storn);
    // printf("%d %d",n,storn);///
    if (n == 0 && storn == 0)
      break;
    for (i = 0; i < n; i++)
      t[i] = 0;
    j = 0;
    s = storn;
    while (1) {
      // printf("%d %d %d %d\\n",j,s,t[j],storn);///
      if (s != 0) {
        t[j]++;
        s--;
      } else if (s == 0) {
        s = t[j];
        t[j] = 0;
      }
      if (t[j] == storn)
        break;
      j++;
      if (j == n)
        j = 0;
    }
    printf("%d\\n", j);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
int main() {
  int t[50], n, s, storn, i, j;
  while (1) {
    scanf("%d %d", &n, &storn);
    // printf("%d %d",n,storn);///
    if (n == 0 && storn == 0)
      break;
    for (i = 0; i < n; i++)
      t[i] = 0;
    j = 0;
    s = storn;
    while (1) {
      printf("%d %d %d %d\\n", j, s, t[j], storn); ///
      if (s != 0) {
        t[j]++;
        s--;
      } else if (s == 0) {
        s = t[j];
        t[j] = 0;
      }
      if (t[j] == storn)
        break;
      j++;
      if (j == n)
        j = 0;
    }
    printf("%d\\n", j);
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int n = 0;
int p = 0;
int have[55] = {};
int winner = 0;
int judge = 0;

int main() {
  while (1) {
    scanf("%d %d", &n, &p);
    if (n == 0 && p == 0) {
      break;
    }
    int pebble = p;
    for (int j = 0; j < n; j++) {
      have[j] = 0;
    }
    while (1) {
      for (int j = 0; j < n; j++) {
        if (pebble != 0) {
          have[j]++;
          pebble--;
          if (have[j] == p) {
            winner = j;
            judge = 1;
            break;
          }
        } else {
          pebble += have[j];
          have[j] = 0;
        }
      }
      if (judge == 1) {
        cout << winner << endl;
        judge = 0;
        break;
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int n[55] = {};
int p[55] = {};
int have[55] = {};
int winner[100] = {};
int judge = 0;

int main() {
  int i = 0;
  while (1) {
    scanf("%d %d", &n[i], &p[i]);
    if (n[i] == 0 && p[i] == 0) {
      break;
    }
    i++;
  }
  int times = i;

  for (i = 0; i < times; i++) {
    int pebble = p[i];
    for (int j = 0; j < n[i]; j++) {
      have[j] = 0;
    }
    while (1) {
      for (int j = 0; j < n[i]; j++) {
        if (pebble != 0) {
          have[j]++;
          pebble--;
          if (have[j] == p[i]) {
            winner[i] = j;
            judge = 1;
            break;
          }
        } else {
          pebble += have[j];
          have[j] = 0;
        }
      }
      if (judge == 1) {
        cout << winner[i] << endl;
        judge = 0;
        break;
      }
    }
  }

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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define REP(i, k, n) for (int i = k; i < n; i++)

#define vi vector<int>
#define pb push_back
using namespace std;
typedef long long ll;

int main() {
  int n, p;
  while (cin >> n >> p, n) {
    int ar[51] = {};
    int i = 0;
    int np = p;
    while (true) {
      if (np != 0) {
        np--;
        ar[i % n]++;
        if (ar[i % n] == p) {
          cout << i % n << endl;
          break;
        }
      } else {
        np = ar[i % n];
        ar[i % n] = 0;
      }
      i++;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define REP(i, k, n) for (int i = k; i < (int)(n); i++)
using namespace std;

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

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

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

int main() {
  while (true) {
    cin >> w >> h;
    if (w == 0)
      break;
    rep(y, h) rep(x, w) { cin >> field[y][x]; }
    int ans = 0;
    rep(y, h) rep(x, w) {
      if (field[y][x] == 1) {
        dfs(y, x);
        ans++;
      }
    }
    cout << ans << endl;
  }

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

#include <iostream>

using namespace std;

int main() {
  int P, N, ndata[55], wan, lop, a;
  while (1) {
    cin >> N;
    cin >> P;
    if (N == 0 && P == 0) {
      break;
    }
    for (lop = 0; lop < 50; lop++) {
      ndata[lop] = 0;
    }
    lop = 0;
    wan = P;
    while (1) {
      /*if(N==3&&P==3){
        lop=0;
        cout<<lop<<endl;
        break;
      }*/
      for (lop = 0; lop < N; lop++) {
        if (wan != 0) {
          wan--;
          ndata[lop]++;
        } else {
          wan = wan + ndata[lop];
          ndata[lop] = 0;
        }
        /*cout<<"wan="<<wan<<endl;
        for(int lop2=0;lop2<N;lop2++){
          cout<<lop2<<"  "<<ndata[lop2]<<endl;
        }
        cout<<endl;
        cin>>a;*/
        if (ndata[lop] == P && wan == 0) {
          break;
        }
      }
      if (ndata[lop] == P && wan == 0) {
        cout << lop << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int P, N, ndata[55], wan, lop, a;
  while (1) {
    cin >> N;
    cin >> P;
    if (N == 0 && P == 0) {
      break;
    }
    for (lop = 0; lop < N; lop++) {
      ndata[lop] = 0;
    }
    lop = 0;
    wan = P;
    while (1) {
      /*if(N==3&&P==3){
        lop=0;
        cout<<lop<<endl;
        break;
      }*/
      for (lop = 0; lop < N; lop++) {
        if (wan != 0) {
          wan--;
          ndata[lop]++;
        } else {
          wan = wan + ndata[lop];
          ndata[lop] = 0;
        }
        cout << "wan=" << wan << endl;
        for (int lop2 = 0; lop2 < N; lop2++) {
          cout << lop2 << "  " << ndata[lop2] << endl;
        }
        cout << endl;
        cin >> a;
        if (ndata[lop] == P && wan == 0) {
          break;
        }
      }
      if (ndata[lop] == P && wan == 0) {
        cout << lop << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p00740 (C++) — buggy submission, verdict: Time Limit Exceeded

#define _GLIBCXX_DEBUG // vectorの範囲外アクセスなどのエラーメッセージが分かりやすくなるらしい

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <algorithm>

// using namespace std;
#include <numeric>

#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

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

#define FOR(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define EXIST(e, s) ((s).find(e) != (s).end())
#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))

#define ll long long

using namespace std;

int main(void) {
  int n;
  int p;

  int temoto[50];
  int wan;
  bool flag;

  while (cin >> n >> p, n || p) {
    flag = false;
    memset(temoto, 0, sizeof(temoto[0]) * 50);
    wan = p;
    while (1) {
      for (int i = 0; i < n; i++) {
        if (wan > 0) {
          wan--;
          temoto[i]++;
          if (temoto[i] == p) {
            cout << i << endl;
            flag = true;
            break;
          }
        } else {
          wan += temoto[i];
          temoto[i] = 0;
        }
      }
      if (flag) {
        break;
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG // vectorの範囲外アクセスなどのエラーメッセージが分かりやすくなるらしい

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include <algorithm>

// using namespace std;
#include <numeric>

#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

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

#define FOR(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

#define EXIST(e, s) ((s).find(e) != (s).end())
#define RESET(a) memset((a), 0, sizeof(a))
#define SET(a) memset((a), -1, sizeof(a))

#define ll long long

using namespace std;

int main(void) {
  int n;
  int p;

  int temoto[50];
  int wan;
  bool flag;

  while (cin >> n >> p, n || p) {
    flag = false;
    while (1) {
      for (int i = 0; i < n; i++) {
        if (wan > 0) {
          wan--;
          temoto[i]++;
          if (temoto[i] == p) {
            cout << i;
            flag = true;
            break;
          }
        } else {
          wan += temoto[i];
        }
      }
      if (flag) {
        break;
      }
    }
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, pmax;
    int stone[50] = {};

    cin >> n >> pmax;
    if (!n && !pmax)
      break;

    int i;
    int p = pmax;
    for (i = 0; 1; i++) {
      if (p) {
        p--;
        stone[i % n]++;
        if (!p && stone[i % n] == pmax) {
          break;
        }
      } else {
        p += stone[i % n];
        stone[i % n] = 0;
      }
    }

    cout << i % n << endl;
  }

  return 0;
}


Error: -8

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  while (true) {
    int n, pmax;
    int stone[50] = {};

    cin >> n >> pmax;

    int i;
    int p = pmax;
    for (i = 0; 1; i++) {
      if (p) {
        p--;
        stone[i % n]++;
        if (!p && stone[i % n] == pmax) {
          break;
        }
      } else {
        p += stone[i % n];
        stone[i % n] = 0;
      }
    }

    cout << i % n << endl;
  }

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

#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {-1, 0, 1, 0};
const int ty[] = {0, 1, 0, -1};

bool check(map<int, int> &hand, int step, int n) {
  for (map<int, int>::iterator it = hand.begin(); it != hand.end(); it++) {
    if (it->first == step % n)
      continue;
    if (it->second != 0)
      return false;
  }
  return true;
}
int main() {
  int n, p;
  while (~scanf("%d %d", &n, &p)) {
    if (n == p && p == 0)
      break;
    map<int, int> hand;
    int bowl = p;
    int step = 0;
    int res = 0;
    while (1) {
      if (bowl > 0) {
        hand[step]++;
        bowl--;
        if (bowl == 0 && check(hand, step, n)) {
          res = step;
          break;
        }
      } else if (bowl == 0) {
        bowl += hand[step];
        hand[step] = 0;
      }
      step = (step + 1) % n;
    }

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

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 100000000

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

static const double EPS = 1e-8;

const int tx[] = {-1, 0, 1, 0};
const int ty[] = {0, 1, 0, -1};

bool check(map<int, int> hand, int step, int n) {
  for (map<int, int>::iterator it = hand.begin(); it != hand.end(); it++) {
    if (it->first == step % n)
      continue;
    if (it->second != 0)
      return false;
  }
  return true;
}
int main() {
  int n, p;
  while (~scanf("%d %d", &n, &p)) {
    if (n == p && p == 0)
      break;
    map<int, int> hand;
    int bowl = p;
    int step = 0;
    int res = 0;
    while (1) {
      if (bowl > 0) {
        hand[step]++;
        bowl--;
        if (bowl == 0 && check(hand, step, n)) {
          res = step;
          break;
        }
      } else if (bowl == 0) {
        bowl += hand[step];
        hand[step] = 0;
      }
      step = (step + 1) % n;
    }

    printf("%d\\n", res);
  }
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#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};

void dfs(int x, int y, vector<vector<bool>> &canGo) {
  canGo[x][y] = false;
  for (int i = 0; i < 8; i++)
    if (canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }

    int input;
    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 count = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (canGo[i][j]) {
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#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};

void dfs(int x, int y, vector<vector<bool>> &canGo) {
  canGo[x][y] = false;
  for (int i = 0; i < 9; i++)
    if (canGo[x + dx[i]][y + dy[i]])
      dfs(x + dx[i], y + dy[i], canGo);
  return;
}

int main(void) {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }

    int input;
    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 count = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        if (canGo[i][j]) {
          ++count;
          dfs(i, j, canGo);
        }
      }
    }

    cout << count << endl;
  }

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

#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

bool dfs(int sx, int sy, vector<vector<bool>> &c) {
  if (c[sx][sy] == false)
    return false;
  c[sx][sy] = false;
  for (int i = 0; i < 8; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    bool flag = dfs(nx, ny, c);
  }
  return true;
}

int main(void) {
  int h, w, temp;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    vector<vector<bool>> c(h + 2, vector<bool>(w + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> temp;
        if (temp == 1)
          c[i][j] = true;
      }
    }
    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        bool flag = dfs(i, j, c);
        if (flag)
          t++;
      }
    }
    cout << t << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;
int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};

bool dfs(int sx, int sy, vector<vector<bool>> &c) {
  if (c[sx][sy] == false)
    return false;
  c[sx][sy] = false;
  for (int i = 0; i < 8; i++) {
    int nx = sx + dx[i];
    int ny = sy + dy[i];
    bool flag = dfs(nx, ny, c);
  }
  return true;
}

int main(void) {
  int h, w, temp;
  while (1) {
    cin >> w >> h;
    if (w == 0)
      break;
    vector<vector<bool>> c(w + 2, vector<bool>(h + 2, false));
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        cin >> temp;
        if (temp == 1)
          c[i][j] = true;
      }
    }
    int t = 0;
    for (int i = 1; i <= h; i++) {
      for (int j = 1; j <= w; j++) {
        bool flag = dfs(i, j, c);
        if (flag)
          t++;
      }
    }
    cout << t << endl;
  }
  return 0;
}
Problem p00741 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define REP(i, N) for (ll i = 0; i < N; ++i)
#define FOR(i, a, b) for (ll i = a; i < b; ++i)
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define INF 1000000000
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
int qx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int qy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

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

void hoge(int wo, int ho) {
  mp[wo][ho] = 0;
  REP(i, 8) {
    int nw = wo + qx[i];
    int nh = ho + qy[i];
    if (nw >= 0 && nw < w && nh >= 0 && nh < h && mp[nw][nh] == 1)
      hoge(nw, nh);
  }
}
int main(void) {
  while (true) {
    ll count = 0;
    cin >> w >> h;
    if (w == 0 && h == 0)
      return 0;
    swap(w, h);
    mp.resize(w);
    REP(i, w) mp[i].resize(h);
    REP(i, w) {
      REP(j, h) { cin >> mp[i][j]; }
    }
    REP(i, w) {
      REP(j, h) {
        if (mp[i][j] == 1) {
          count++;
          hoge(i, j);
        }
      }
    }
    cout << count << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;
const int MOD = 1e9 + 7;

int c[100][100]; // 0　海
int h, w;

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

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

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

  while (true) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        cin >> c[i][j];
      }
    int res = 0;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++) {
        if (c[i][j] == 1) {
          bfs(i, j);
          res++;
        }
      }
    cout << res << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
int x[90][90], col[90][90], h, w, c, i, j, k, l, p, q;
queue<int> Q, R;
int main() {
  while (true) {
    c = 0;
    memset(x, 0, sizeof(x));
    memset(col, 0, sizeof(col));
    cin >> w >> h;
    if (h == 0) {
      break;
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        cin >> x[i][j];
      }
    }
    for (i = 0; i < h; i++) {
      for (j = 0; j < w; j++) {
        if (x[i][j] == 1 && col[i][j] == 0) {
          c++;
          col[i][j] = c;
          Q.push(i * 100 + j);
          while (!Q.empty()) {
            while (!Q.empty()) {
              p = Q.front() / 100;
              q = Q.front() % 100;
              for (k = -1; k < 2; k++) {
                for (l = -1; l < 2; l++) {
                  if (x[p + k][q + l] == 1 && col[p + k][q + l] == 0) {
                    col[p + k][q + l] = c;
                    R.push((p + k) * 100 + q + l);
                  }
                }
              }
              Q.pop();
            }
            while (!R.empty()) {
              Q.push(R.front());
              R.pop();
            }
          }
        }
      }
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
int H, W, k[100][100], n[100][100], color = 1, i, j, l, m, o, r;
int main() {
  while (true) {
    memset(n, 0, sizeof(n));
    r = 0;
    cin >> H >> W;
    if (H == 0) {
      break;
    }
    for (i = 0; i < H; i++) {
      for (j = 0; j < W; j++) {
        cin >> k[i][j];
      }
    }
    for (i = 0; i < H; i++) {
      for (j = 0; j < W; j++) {
        if (r == 1) {
          color++;
        }
        r = 0;
        if (k[i][j] == 1 && n[i][j] == 0) {
          n[i][j] = color;
          for (l = 0; l < 100; l++) {
            for (m = 0; m < H; m++) {
              for (o = 0; o < W; o++) {
                if (n[m][o] == color) {
                  for (int p = -1; p <= 1; p++) {
                    for (int q = -1; q <= 1; q++) {
                      if (m - p >= 0 && m - p < H) {
                        if (o - q >= 0 && o - q < W) {
                          n[m][o] = color;
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    cout << color << endl;
  }
}
Problem p00741 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int c[100][100]; // 0??????
int h, w;

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

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

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

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

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int c[100][100]; // 0??????
int h, w;

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

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

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

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