Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
#define MAX 1LL << 60

typedef long long ll;

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repR(i, s, e) for (ll i = s; i > e; i--)
#define reft0(i, j) setfill('0') << setw(i) << j

template <class T> inline bool chMax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chMin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef struct {
  bool bakudan;
  int num;
} masu;
int main() {
  ll H, W;
  masu S[50][50] = {0};
  cin >> H >> W;
  rep(i, 0, H) {
    rep(ii, 0, W) {
      char temp;
      cin >> temp;
      if (temp == '#') {
        S[i][ii].bakudan = true;
      }
    }
  }
  rep(i, 0, H) {
    rep(ii, 0, W) {
      if (S[i][ii].bakudan == true) {
        S[i + 1][ii].num++;
        S[i][ii + 1].num++;
        S[i + 1][ii + 1].num++;
        if (i != 0) {
          S[i - 1][ii].num++;
          S[i - 1][ii + 1].num++;
        }
        if (ii != 0) {
          S[i][ii - 1].num++;
          S[i + 1][ii - 1].num++;
        }
        if (i != 0 && ii != 0) {
          S[i - 1][ii - 1].num++;
        }
      }
    }
  }
  rep(i, 0, H) {
    rep(ii, 0, W) {
      if (S[i][ii].bakudan == true) {
        cout << '#';
      } else {
        cout << S[i][ii].num;
      }
    }
    cout << endl;
  }
  return 0;
}


Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <ccomplex>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <numeric>
#include <set>
#include <sstream>
#include <tuple>
#include <unordered_map>
#include <vector>

using namespace std;
#define MAX 1LL << 60

typedef long long ll;

#define rep(i, s, e) for (ll i = s; i < e; i++)
#define repR(i, s, e) for (ll i = s; i > e; i--)
#define reft0(i, j) setfill('0') << setw(i) << j

template <class T> inline bool chMax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chMin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

typedef struct {
  bool bakudan;
  int num;
} masu;
int main() {
  ll H, W;
  masu S[60][60] = {0};
  cin >> H >> W;
  rep(i, 0, H) {
    rep(ii, 0, W) {
      char temp;
      cin >> temp;
      if (temp == '#') {
        S[i][ii].bakudan = true;
      }
    }
  }
  rep(i, 0, H) {
    rep(ii, 0, W) {
      if (S[i][ii].bakudan == true) {
        S[i + 1][ii].num++;
        S[i][ii + 1].num++;
        S[i + 1][ii + 1].num++;
        if (i != 0) {
          S[i - 1][ii].num++;
          S[i - 1][ii + 1].num++;
        }
        if (ii != 0) {
          S[i][ii - 1].num++;
          S[i + 1][ii - 1].num++;
        }
        if (i != 0 && ii != 0) {
          S[i - 1][ii - 1].num++;
        }
      }
    }
  }
  rep(i, 0, H) {
    rep(ii, 0, W) {
      if (S[i][ii].bakudan == true) {
        cout << '#';
      } else {
        cout << S[i][ii].num;
      }
    }
    cout << endl;
  }
  return 0;
}