Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

ll solve(const vector<string> &S, ll H, ll W, ll h, ll w) {
  if (S[h][w] == '#') {
    return -1;
  }

  ll sum = 0;
  REP(i, h - 1, h + 2) {
    REP(j, w - 1, w + 2) {
      if (i == h && j == w) {
        continue;
      }
      if (i < 0 || H < i || j < 0 || W < j) {
        continue;
      }
      if (S[i][j] == '#') {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  init();

  ll H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, 0, H) {
    string s;
    cin >> s;
    S[i] = s;
  }

  REP(h, 0, H) {
    REP(w, 0, W) {
      ll s = solve(S, H, W, h, w);
      if (s == -1) {
        cout << "#";
      } else {
        cout << s;
      }
    }
    cout << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define REP(X, Y, Z) for (ll(X) = (Y); (X) < (Z); (X)++)

using namespace std;
using ll = long long;
using dll = deque<ll>;
using pll = pair<ll, ll>;
using qll = queue<ll>;
using usll = unordered_set<ll>;
using umll = unordered_map<ll, ll>;
using sll = stack<int>;
using vll = vector<ll>;
using vvll = vector<vll>;

constexpr ll kMod = 1e9 + 7;

ll gcd(ll a, ll b) {
  if (a < b) {
    return gcd(b, a);
  }
  ll c = a % b;
  while (c != 0) {
    a = b;
    b = c;
    c = a % b;
  }
  return b;
}

ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

void init() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
}

ll solve(const vector<string> &S, ll H, ll W, ll h, ll w) {
  if (S[h][w] == '#') {
    return -1;
  }

  ll sum = 0;
  REP(i, h - 1, h + 2) {
    REP(j, w - 1, w + 2) {
      if (i == h && j == w) {
        continue;
      }
      if (i < 0 || H <= i || j < 0 || W <= j) {
        continue;
      }
      if (S[i][j] == '#') {
        sum++;
      }
    }
  }
  return sum;
}

int main() {
  init();

  ll H, W;
  cin >> H >> W;
  vector<string> S(H);
  REP(i, 0, H) {
    string s;
    cin >> s;
    S[i] = s;
  }

  REP(h, 0, H) {
    REP(w, 0, W) {
      ll s = solve(S, H, W, h, w);
      if (s == -1) {
        cout << "#";
      } else {
        cout << s;
      }
    }
    cout << endl;
  }

  return 0;
}