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 repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, H) { cin >> S[i]; }
  vector<vector<int>> mine(H, vector<int>(W));
  rep(i, H) {
    rep(j, W) {
      if (S[i][j] == '.') {
        mine[i][j] = 0;
      } else if (S[i][j] == '#') {
        mine[i][j] = -10;
      }
    }
  }
  if (S[0][1] == '#') {
    mine[0][0]++;
  }
  if (S[1][0] == '#') {
    mine[0][0]++;
  }
  if (S[1][1] == '#') {
    mine[0][0]++;
  }
  for (int i = 1; i < W - 1; i++) {
    if (S[0][i - 1] == '#') {
      mine[0][i]++;
    }
    if (S[0][i + 1] == '#') {
      mine[0][i]++;
    }
    if (S[1][i - 1] == '#') {
      mine[0][i]++;
    }
    if (S[1][i] == '#') {
      mine[0][i]++;
    }
    if (S[1][i + 1] == '#') {
      mine[0][i]++;
    }
  }
  if (S[0][W - 2] == '#') {
    mine[0][W - 1]++;
  }
  if (S[1][W - 2] == '#') {
    mine[0][W - 1]++;
  }
  if (S[1][W - 1] == '#') {
    mine[0][W - 1]++;
  }
  for (int i = 1; i < H - 1; i++) {
    if (S[i - 1][0] == '#') {
      mine[i][0]++;
    }
    if (S[i - 1][1] == '#') {
      mine[i][0]++;
    }
    if (S[i][1] == '#') {
      mine[i][0]++;
    }
    if (S[i + 1][0] == '#') {
      mine[i][0]++;
    }
    if (S[i + 1][1] == '#') {
      mine[i][0]++;
    }
  }
  for (int i = 1; i < H - 1; i++) {
    if (S[i - 1][W - 1] == '#') {
      mine[i][W - 1]++;
    }
    if (S[i - 1][W - 2] == '#') {
      mine[i][W - 1]++;
    }
    if (S[i][W - 2] == '#') {
      mine[i][W - 1]++;
    }
    if (S[i + 1][W - 1] == '#') {
      mine[i][W - 1]++;
    }
    if (S[i + 1][W - 2] == '#') {
      mine[i][W - 1]++;
    }
  }
  for (int i = 1; i < H - 1; i++) {
    for (int j = 1; j < W - 1; j++) {
      if (S[i - 1][j - 1] == '#') {
        mine[i][j]++;
      }
      if (S[i - 1][j] == '#') {
        mine[i][j]++;
      }
      if (S[i - 1][j + 1] == '#') {
        mine[i][j]++;
      }
      if (S[i][j - 1] == '#') {
        mine[i][j]++;
      }
      if (S[i][j + 1] == '#') {
        mine[i][j]++;
      }
      if (S[i + 1][j] == '#') {
        mine[i][j]++;
      }
      if (S[i + 1][j - 1] == '#') {
        mine[i][j]++;
      }
      if (S[i + 1][j + 1] == '#') {
        mine[i][j]++;
      }
    }
  }
  if (S[H - 2][0] == '#') {
    mine[H - 1][0]++;
  }
  if (S[H - 2][1] == '#') {
    mine[H - 1][0]++;
  }
  if (S[H - 1][1] == '#') {
    mine[H - 1][0]++;
  }
  if (S[H - 2][W - 2] == '#') {
    mine[H - 1][W - 1]++;
  }
  if (S[H - 2][W - 1] == '#') {
    mine[H - 1][W - 1]++;
  }
  if (S[H - 1][W - 2] == '#') {
    mine[H - 1][W - 1]++;
  }
  for (int i = 1; i < W - 1; i++) {
    if (S[H - 1][i - 1] == '#') {
      mine[H - 1][i]++;
    }
    if (S[H - 1][i + 1] == '#') {
      mine[H - 1][i]++;
    }
    if (S[H - 2][i - 1] == '#') {
      mine[H - 1][i]++;
    }
    if (S[H - 2][i] == '#') {
      mine[H - 1][i]++;
    }
    if (S[H - 2][i + 1] == '#') {
      mine[H - 1][i]++;
    }
  }
  rep(i, H) {
    rep(j, W - 1) {
      if (mine[i][j] >= 0) {
        cout << mine[i][j];
      } else {
        cout << "#";
      }
    }
    if (mine[i][W - 1] >= 0) {
      cout << mine[i][W - 1] << endl;
    } else {
      cout << "#" << endl;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;

int main() {
  int H, W;
  cin >> H >> W;
  vector<string> S(H);
  rep(i, H) { cin >> S[i]; }
  vector<vector<int>> mine(H, vector<int>(W));
  rep(i, H) {
    rep(j, W) {
      if (S[i][j] == '.') {
        mine[i][j] = 0;
      } else if (S[i][j] == '#') {
        mine[i][j] = -10;
      }
    }
  }
  if (H >= 2 && W >= 2) {
    if (S[0][1] == '#') {
      mine[0][0]++;
    }
    if (S[1][0] == '#') {
      mine[0][0]++;
    }
    if (S[1][1] == '#') {
      mine[0][0]++;
    }
    for (int i = 1; i < W - 1; i++) {
      if (S[0][i - 1] == '#') {
        mine[0][i]++;
      }
      if (S[0][i + 1] == '#') {
        mine[0][i]++;
      }
      if (S[1][i - 1] == '#') {
        mine[0][i]++;
      }
      if (S[1][i] == '#') {
        mine[0][i]++;
      }
      if (S[1][i + 1] == '#') {
        mine[0][i]++;
      }
    }
    if (S[0][W - 2] == '#') {
      mine[0][W - 1]++;
    }
    if (S[1][W - 2] == '#') {
      mine[0][W - 1]++;
    }
    if (S[1][W - 1] == '#') {
      mine[0][W - 1]++;
    }
    for (int i = 1; i < H - 1; i++) {
      if (S[i - 1][0] == '#') {
        mine[i][0]++;
      }
      if (S[i - 1][1] == '#') {
        mine[i][0]++;
      }
      if (S[i][1] == '#') {
        mine[i][0]++;
      }
      if (S[i + 1][0] == '#') {
        mine[i][0]++;
      }
      if (S[i + 1][1] == '#') {
        mine[i][0]++;
      }
    }
    for (int i = 1; i < H - 1; i++) {
      if (S[i - 1][W - 1] == '#') {
        mine[i][W - 1]++;
      }
      if (S[i - 1][W - 2] == '#') {
        mine[i][W - 1]++;
      }
      if (S[i][W - 2] == '#') {
        mine[i][W - 1]++;
      }
      if (S[i + 1][W - 1] == '#') {
        mine[i][W - 1]++;
      }
      if (S[i + 1][W - 2] == '#') {
        mine[i][W - 1]++;
      }
    }
    for (int i = 1; i < H - 1; i++) {
      for (int j = 1; j < W - 1; j++) {
        if (S[i - 1][j - 1] == '#') {
          mine[i][j]++;
        }
        if (S[i - 1][j] == '#') {
          mine[i][j]++;
        }
        if (S[i - 1][j + 1] == '#') {
          mine[i][j]++;
        }
        if (S[i][j - 1] == '#') {
          mine[i][j]++;
        }
        if (S[i][j + 1] == '#') {
          mine[i][j]++;
        }
        if (S[i + 1][j] == '#') {
          mine[i][j]++;
        }
        if (S[i + 1][j - 1] == '#') {
          mine[i][j]++;
        }
        if (S[i + 1][j + 1] == '#') {
          mine[i][j]++;
        }
      }
    }
    if (S[H - 2][0] == '#') {
      mine[H - 1][0]++;
    }
    if (S[H - 2][1] == '#') {
      mine[H - 1][0]++;
    }
    if (S[H - 1][1] == '#') {
      mine[H - 1][0]++;
    }
    if (S[H - 2][W - 2] == '#') {
      mine[H - 1][W - 1]++;
    }
    if (S[H - 2][W - 1] == '#') {
      mine[H - 1][W - 1]++;
    }
    if (S[H - 1][W - 2] == '#') {
      mine[H - 1][W - 1]++;
    }
    for (int i = 1; i < W - 1; i++) {
      if (S[H - 1][i - 1] == '#') {
        mine[H - 1][i]++;
      }
      if (S[H - 1][i + 1] == '#') {
        mine[H - 1][i]++;
      }
      if (S[H - 2][i - 1] == '#') {
        mine[H - 1][i]++;
      }
      if (S[H - 2][i] == '#') {
        mine[H - 1][i]++;
      }
      if (S[H - 2][i + 1] == '#') {
        mine[H - 1][i]++;
      }
    }
  } else if (H == 1) {
    if (S[0][1] == '#') {
      mine[0][0]++;
    }
    for (int i = 1; i < W - 1; i++) {
      if (S[0][i - 1] == '#') {
        mine[0][i]++;
      }
      if (S[0][i + 1] == '#') {
        mine[0][i]++;
      }
    }
    if (S[0][W - 2] == '#') {
      mine[0][W - 1]++;
    }
  } else if (W == 1) {
    if (S[1][0] == '#') {
      mine[0][0]++;
    }
    for (int i = 1; i < H - 1; i++) {
      if (S[i - 1][0] == '#') {
        mine[i][0]++;
      }
      if (S[i + 1][0] == '#') {
        mine[i][0]++;
      }
    }
    if (S[H - 2][0] == '#') {
      mine[H - 1][0]++;
    }
  }
  rep(i, H) {
    rep(j, W - 1) {
      if (mine[i][j] >= 0) {
        cout << mine[i][j];
      } else {
        cout << "#";
      }
    }
    if (mine[i][W - 1] >= 0) {
      cout << mine[i][W - 1] << endl;
    } else {
      cout << "#" << endl;
    }
  }
}