Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repone(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define Rep(j, n) for (int j = 0; j < (int)(n); ++j)
#define Repo(j, o, n) for (int j = o; j < (int)(n); ++j)
#define Repone(j, n) for (int j = 1; j < (int)(n); ++j)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define int long long
#define pb(a) push_back(a)
#define eraser(v, i) (v).erase(v.begin() + (i))
#define pbpair(a, b) push_back(make_pair(a, b))
#define Little 1
const long long INF = 1LL << 60;
#define pairint pair<int, int>
signed main() {
  int x = 0, y = 0, z = 0, w = 0, n = 0, m = 0, o = 0;
  std::cin >> x >> y;
  std::vector<string> v(x);
  rep(i, x) std::cin >> v[i];
  rep(i, x) {
    Rep(j, y) {
      if (v[i][j] == '#')
        continue;
      if (i == 0 && j == 0) {
        if (x > 1)
          if (v[1][0] == '#')
            n++;
        if (v[1][1] == '#')
          n++;
        if (y > 1)
          if (v[0][1] == '#')
            n++;
      } else if (i == 0 && j == y - 1) {
        if (y > 2)
          if (v[0][y - 2] == '#')
            n++;
        if (x > 1 && y > 2)
          if (v[1][y - 2] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][y - 1] == '#')
            n++;
      } else if (i == 0) {
        if (y > 1)
          if (v[0][j - 1] == '#')
            n++;
        if (y > 1)
          if (v[0][j + 1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][j + 1] == '#')
            n++;
      } else if (i == x - 1 && j == 0) {
        if (x > 1)
          if (v[i - 1][0] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][1] == '#')
            n++;
        if (y > 1)
          if (v[i][1] == '#')
            n++;
      } else if (j == 0) {
        if (y > 1)
          if (v[i][1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][1] == '#')
            n++;
        if (x > 1)
          if (v[i - 1][0] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i + 1][1] == '#')
            n++;
        if (x > 1)
          if (v[i + 1][0] == '#')
            n++;
      } else if (i == x - 1 && j == y - 1) {
        if (x > 2 && y > 1)
          if (v[x - 2][y - 1] == '#')
            n++;
        if (x > 2 && y > 2)
          if (v[x - 2][y - 2] == '#')
            n++;
        if (x > 1 && y > 2)
          if (v[x - 1][y - 2] == '#')
            n++;
      } else if (i == x - 1) {
        if (x > 1)
          if (v[i - 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j - 1] == '#')
            n++;
        if (y > 1)
          if (v[i][j - 1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j + 1] == '#')
            n++;
        if (y > 1)
          if (v[i][j + 1] == '#')
            n++;
      } else if (j == y - 1) {
        if (y > 1)
          if (v[i][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[i - 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[i + 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i + 1][j - 1] == '#')
            n++;
      } else {
        if (v[i - 1][j - 1] == '#')
          n++;
        if (v[i - 1][j] == '#')
          n++;
        if (v[i - 1][j + 1] == '#')
          n++;
        if (v[i][j - 1] == '#')
          n++;
        if (v[i][j + 1] == '#')
          n++;
        if (v[i + 1][j - 1] == '#')
          n++;
        if (v[i + 1][j] == '#')
          n++;
        if (v[i + 1][j + 1] == '#')
          n++;
      }
      v[i][j] = char('0' + n);
      n = 0;
    }
  }
  rep(i, x) std::cout << v[i] << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define repone(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define Rep(j, n) for (int j = 0; j < (int)(n); ++j)
#define Repo(j, o, n) for (int j = o; j < (int)(n); ++j)
#define Repone(j, n) for (int j = 1; j < (int)(n); ++j)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define int long long
#define pb(a) push_back(a)
#define eraser(v, i) (v).erase(v.begin() + (i))
#define pbpair(a, b) push_back(make_pair(a, b))
#define Little 1
const long long INF = 1LL << 60;
#define pairint pair<int, int>
signed main() {
  int x = 0, y = 0, z = 0, w = 0, n = 0, m = 0, o = 0;
  std::cin >> x >> y;
  std::vector<string> v(x);
  rep(i, x) std::cin >> v[i];
  rep(i, x) {
    Rep(j, y) {
      if (v[i][j] == '#')
        continue;
      if (i == 0 && j == 0) {
        if (x > 1)
          if (v[1][0] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][1] == '#')
            n++;
        if (y > 1)
          if (v[0][1] == '#')
            n++;
      } else if (i == 0 && j == y - 1) {
        if (y > 2)
          if (v[0][y - 2] == '#')
            n++;
        if (x > 1 && y > 2)
          if (v[1][y - 2] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][y - 1] == '#')
            n++;
      } else if (i == 0) {
        if (y > 1)
          if (v[0][j - 1] == '#')
            n++;
        if (y > 1)
          if (v[0][j + 1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[1][j + 1] == '#')
            n++;
      } else if (i == x - 1 && j == 0) {
        if (x > 1)
          if (v[i - 1][0] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][1] == '#')
            n++;
        if (y > 1)
          if (v[i][1] == '#')
            n++;
      } else if (j == 0) {
        if (y > 1)
          if (v[i][1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][1] == '#')
            n++;
        if (x > 1)
          if (v[i - 1][0] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i + 1][1] == '#')
            n++;
        if (x > 1)
          if (v[i + 1][0] == '#')
            n++;
      } else if (i == x - 1 && j == y - 1) {
        if (x > 2 && y > 1)
          if (v[x - 2][y - 1] == '#')
            n++;
        if (x > 2 && y > 2)
          if (v[x - 2][y - 2] == '#')
            n++;
        if (x > 1 && y > 2)
          if (v[x - 1][y - 2] == '#')
            n++;
      } else if (i == x - 1) {
        if (x > 1)
          if (v[i - 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j - 1] == '#')
            n++;
        if (y > 1)
          if (v[i][j - 1] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j + 1] == '#')
            n++;
        if (y > 1)
          if (v[i][j + 1] == '#')
            n++;
      } else if (j == y - 1) {
        if (y > 1)
          if (v[i][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[i - 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i - 1][j - 1] == '#')
            n++;
        if (x > 1)
          if (v[i + 1][j] == '#')
            n++;
        if (x > 1 && y > 1)
          if (v[i + 1][j - 1] == '#')
            n++;
      } else {
        if (v[i - 1][j - 1] == '#')
          n++;
        if (v[i - 1][j] == '#')
          n++;
        if (v[i - 1][j + 1] == '#')
          n++;
        if (v[i][j - 1] == '#')
          n++;
        if (v[i][j + 1] == '#')
          n++;
        if (v[i + 1][j - 1] == '#')
          n++;
        if (v[i + 1][j] == '#')
          n++;
        if (v[i + 1][j + 1] == '#')
          n++;
      }
      v[i][j] = char('0' + n);
      n = 0;
    }
  }
  rep(i, x) std::cout << v[i] << '\n';
}