Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;

  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      if (i == 0) {
        if (j == 0) {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i][j + 1] == '#') +
                    (s[i + 1][j + 1] == '#');
        } else if (j == w - 1) {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i][j - 1] == '#') +
                    (s[i + 1][j - 1] == '#');
        } else {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i][j + 1] == '#') +
                    (s[i + 1][j + 1] == '#') + (s[i][j - 1] == '#') +
                    (s[i + 1][j - 1] == '#');
        }
      } else if (i == h - 1) {
        if (j == 0) {
          s[i][j] = '0' + (s[i - 1][j] == '#') + (s[i][j + 1] == '#') +
                    (s[i - 1][j + 1] == '#');
        } else if (j == w - 1) {
          s[i][j] = '0' + (s[i - 1][j] == '#') + (s[i][j - 1] == '#') +
                    (s[i - 1][j - 1] == '#');
        } else {
          s[i][j] = '0' + (s[i - 1][j] == '#') + (s[i][j + 1] == '#') +
                    (s[i - 1][j + 1] == '#') + (s[i][j - 1] == '#') +
                    (s[i - 1][j - 1] == '#');
        }
      } else {
        if (j == 0) {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i - 1][j] == '#') +
                    (s[i][j + 1] == '#') + (s[i + 1][j + 1] == '#') +
                    (s[i - 1][j + 1] == '#');
        } else if (j == w - 1) {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i - 1][j] == '#') +
                    (s[i][j - 1] == '#') + (s[i + 1][j - 1] == '#') +
                    (s[i - 1][j - 1] == '#');
        } else {
          s[i][j] = '0' + (s[i + 1][j] == '#') + (s[i - 1][j] == '#') +
                    (s[i][j + 1] == '#') + (s[i + 1][j + 1] == '#') +
                    (s[i - 1][j + 1] == '#') + (s[i][j - 1] == '#') +
                    (s[i + 1][j - 1] == '#') + (s[i - 1][j - 1] == '#');
        }
      }
    }
  }

  for (int i = 0; i < h; i++)
    cout << s[i] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  int h, w;
  cin >> h >> w;

  vector<string> s(h);
  for (int i = 0; i < h; i++) {
    cin >> s[i];
  }

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] == '#')
        continue;
      s[i][j] = '0';
      if (i - 1 >= 0 && j - 1 >= 0)
        s[i][j] += (s[i - 1][j - 1] == '#');
      if (i - 1 >= 0)
        s[i][j] += (s[i - 1][j] == '#');
      if (i - 1 >= 0 && j + 1 < w)
        s[i][j] += (s[i - 1][j + 1] == '#');
      if (j + 1 < w)
        s[i][j] += (s[i][j + 1] == '#');
      if (i + 1 < h && j + 1 < w)
        s[i][j] += (s[i + 1][j + 1] == '#');
      if (i + 1 < h)
        s[i][j] += (s[i + 1][j] == '#');
      if (i + 1 < h && j - 1 >= 0)
        s[i][j] += (s[i + 1][j - 1] == '#');
      if (j - 1 >= 0)
        s[i][j] += (s[i][j - 1] == '#');
    }
  }

  for (int i = 0; i < h; i++)
    cout << s[i] << endl;
  return 0;
}