Problem p00535 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PA;
typedef pair<PA, int> PPA;
int main() {
  int h, w;
  char masu[1001][1001];
  int suji[1001][1001] = {};
  queue<PPA> que;
  PA p;
  int sum = 0;
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> masu[i][j];
      if (masu[i][j] == '.') {
        for (int k = -1; k <= 1; k++)
          for (int l = -1; l <= 1; l++) {
            if (k == 0 && l == 0)
              continue;
            int y = i + k;
            int x = j + l;
            if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
              continue;
            suji[y][x]++;
          }
      }
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (suji[i][j] >= masu[i][j] - '0' && masu[i][j] != '.') {
        que.push(PPA(PA(i, j), 1));
      }
    }
  queue<PA> queb;
  while (!que.empty()) {
    p = que.front().first;
    if (sum != que.front().second) {
      while (!queb.empty()) {
        masu[queb.front().first][queb.front().second] = '.';
        queb.pop();
      }
      sum++;
    }
    que.pop();
    queb.push(p);
    for (int k = -1; k <= 1; k++)
      for (int l = -1; l <= 1; l++) {
        if (k == 0 && l == 0)
          continue;
        int y = p.first + k;
        int x = p.second + l;
        if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
          continue;
        suji[y][x]++;
        if (suji[y][x] == masu[y][x] - '0') {
          que.push(PPA(PA(y, x), sum + 1));
        }
      }
  }
  cout << sum << endl;
  return (0);
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> PA;
typedef pair<PA, int> PPA;
int main() {
  int h, w;
  char masu[50][50];
  int suji[50][50] = {};
  queue<PPA> que;
  PA p;
  int sum = 0;
  cin >> h >> w;
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      cin >> masu[i][j];
      if (masu[i][j] == '.') {
        for (int k = -1; k <= 1; k++)
          for (int l = -1; l <= 1; l++) {
            if (k == 0 && l == 0)
              continue;
            int y = i + k;
            int x = j + l;
            if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
              continue;
            suji[y][x]++;
          }
      }
    }
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++) {
      if (suji[i][j] >= masu[i][j] - '0' && masu[i][j] != '.') {
        que.push(PPA(PA(i, j), 1));
      }
    }
  queue<PA> queb;
  while (!que.empty()) {
    p = que.front().first;
    if (sum != que.front().second) {
      while (!queb.empty()) {
        masu[queb.front().first][queb.front().second] = '.';
        queb.pop();
      }
      sum++;
    }
    que.pop();
    queb.push(p);
    for (int k = -1; k <= 1; k++)
      for (int l = -1; l <= 1; l++) {
        if (k == 0 && l == 0)
          continue;
        int y = p.first + k;
        int x = p.second + l;
        if (y < 0 || y >= h || x < 0 || x >= w || masu[y][x] == '.')
          continue;
        suji[y][x]++;
        if (suji[y][x] == masu[y][x] - '0') {
          que.push(PPA(PA(y, x), sum + 1));
        }
      }
  }
  cout << sum << endl;
  return (0);
}