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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[2000][2000];
queue<pii> p1;
queue<pii> p2;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w, h;
  cin >> h >> w;
  memset(a, 0, sizeof(a));
  rep(i, 0, h) {
    rep(j, 0, w) {
      char c;
      cin >> c;
      if (c == '.') {
        p1.push(mp(j, i));
      } else {
        // cout << c-'0' << endl;
        a[i][j] = c - '0';
      }
    }
  }
  int dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
  int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
  int ans = 0;
  while (!p1.empty()) {
    // if(p1.empty())break;
    ans++;
    while (!p1.empty()) {
      pii pa = p1.front();
      p1.pop();
      rep(i, 0, 8) {
        int x = dx[i] + pa.first;
        int y = dy[i] + pa.second;
        if (x < 0 || y < 0 || x >= w || y >= h)
          continue;
        /// a[y][x]--;
        // cout << a[y][x];
        if (a[y][x] == 1) {
          // cout << y << x << endl;
          p2.push(mp(x, y));
        }
        if (a[y][x] > 0)
          a[y][x]--;
      }
    }
    if (p2.empty())
      break;
    ans++;
    while (!p2.empty()) {
      pii pa = p2.front();
      p2.pop();
      rep(i, 0, 8) {
        int x = dx[i] + pa.first;
        int y = dy[i] + pa.second;
        if (x < 0 || y < 0 || x >= w || y >= h)
          continue;
        if (a[y][x] == 1) {
          p1.push(mp(x, y));
          //  cout << y << x << endl;
        }
        if (a[y][x] > 0)
          a[y][x]--;
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
int a[50][2000];
queue<pii> p1;
queue<pii> p2;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int w, h;
  cin >> h >> w;
  memset(a, 0, sizeof(a));
  rep(i, 0, h) {
    rep(j, 0, w) {
      char c;
      cin >> c;
      if (c == '.') {
        p1.push(mp(j, i));
      } else {
        // cout << c-'0' << endl;
        a[i][j] = c - '0';
      }
    }
  }
  int dx[8] = {0, 1, 0, -1, 1, -1, -1, 1};
  int dy[8] = {1, 0, -1, 0, 1, -1, 1, -1};
  int ans = 0;
  while (!p1.empty()) {
    // if(p1.empty())break;
    ans++;
    while (!p1.empty()) {
      pii pa = p1.front();
      p1.pop();
      rep(i, 0, 8) {
        int x = dx[i] + pa.first;
        int y = dy[i] + pa.second;
        if (x < 0 || y < 0 || x >= w || y >= h)
          continue;
        /// a[y][x]--;
        // cout << a[y][x];
        if (a[y][x] == 1) {
          // cout << y << x << endl;
          p2.push(mp(x, y));
        }
        if (a[y][x] > 0)
          a[y][x]--;
      }
    }
    if (p2.empty())
      break;
    ans++;
    while (!p2.empty()) {
      pii pa = p2.front();
      p2.pop();
      rep(i, 0, 8) {
        int x = dx[i] + pa.first;
        int y = dy[i] + pa.second;
        if (x < 0 || y < 0 || x >= w || y >= h)
          continue;
        if (a[y][x] == 1) {
          p1.push(mp(x, y));
          //  cout << y << x << endl;
        }
        if (a[y][x] > 0)
          a[y][x]--;
      }
    }
  }
  cout << ans - 1 << endl;
  return 0;
}