Problem p00535 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-8;
int INF = 2e9;
char fld[55][55];
int cnt[55][55];
bool used[55][55];
int main() {
  int h, w, turn = 0;
  cin >> h >> w;
  REP(i, h) REP(j, w) cin >> fld[j][i];
  memset(used, false, sizeof(used));
  queue<P> Q;
  REP(i, h) REP(j, w) {
    if (fld[j][i] == '.')
      continue;
    for (int x = -1; x <= 1; x++)
      for (int y = -1; y <= 1; y++)
        if (fld[j + x][i + y] == '.')
          cnt[j][i]++;
    if (cnt[j][i] >= (fld[j][i] - '0')) {
      Q.push(P(j, i));
      used[j][i] = true;
    }
  }
  if (Q.size())
    turn++;
  else {
    cout << 0 << endl;
    return 0;
  }
  bool update = true;
  while (update) {
    int ql = Q.size();
    REP(i, ql) {
      P p = Q.front();
      Q.pop();
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (fld[p.first + x][p.second + y] != '.') {
            cnt[p.first + x][p.second + y]++;
            if (cnt[p.first + x][p.second + y] >=
                    (fld[p.first + x][p.second + y] - '0') &&
                !used[p.first + x][p.second + y]) {
              Q.push(P(p.first + x, p.second + y));
              used[p.first + x][p.second + y] = true;
            }
          }
        }
      }
    }
    if (!Q.size()) {
      cout << turn << endl;
      return 0;
    }
    turn++;
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define ALL(a) (a).begin(), (a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
double EPS = 1e-8;
int INF = 2e9;
char fld[1050][1050];
int cnt[1050][1050];
bool used[1050][1050];
int main() {
  int h, w, turn = 0;
  cin >> h >> w;
  REP(i, h) REP(j, w) cin >> fld[j][i];
  memset(used, false, sizeof(used));
  queue<P> Q;
  REP(i, h) REP(j, w) {
    if (fld[j][i] == '.')
      continue;
    for (int x = -1; x <= 1; x++)
      for (int y = -1; y <= 1; y++)
        if (fld[j + x][i + y] == '.')
          cnt[j][i]++;
    if (cnt[j][i] >= (fld[j][i] - '0')) {
      Q.push(P(j, i));
      used[j][i] = true;
    }
  }
  if (Q.size())
    turn++;
  else {
    cout << 0 << endl;
    return 0;
  }
  bool update = true;
  while (update) {
    int ql = Q.size();
    REP(i, ql) {
      P p = Q.front();
      Q.pop();
      for (int x = -1; x <= 1; x++) {
        for (int y = -1; y <= 1; y++) {
          if (fld[p.first + x][p.second + y] != '.') {
            cnt[p.first + x][p.second + y]++;
            if (cnt[p.first + x][p.second + y] >=
                    (fld[p.first + x][p.second + y] - '0') &&
                !used[p.first + x][p.second + y]) {
              Q.push(P(p.first + x, p.second + y));
              used[p.first + x][p.second + y] = true;
            }
          }
        }
      }
    }
    if (!Q.size()) {
      cout << turn << endl;
      return 0;
    }
    turn++;
  }
}