Problem p03574 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
// #include "bits/stdc++.h"
using namespace std;
// using namespace std::vector;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)

#define huge 1000000007
typedef long long ll;
ll gcd(ll a, ll b);

int main(void) {

  ll N = 0;
  ll A = 0;
  ll B = 0;
  ll C = 0;
  ll D = 0;
  ll M = 0;
  // ll M = 0;
  ll a[200] = {};
  ll b[200] = {};
  ll v[3] = {};
  // ll *a, *b, *x;
  string S, T, U[52];
  ll count = 0;
  ll sum = 0;
  ll min = 100000;
  ll max = 0;
  int flag = 0;
  char temp, head;
  // int is = 0;

  vector<int> c(3);
  // char p;
  cin >> A >> B;

  rep(i, 0, A) { cin >> U[i]; }

  rep(j, 0, A) {
    rep(i, 0, B) {

      if (U[j][i] == '.') {

        rep(y, j - 1, j + 2) {
          rep(x, i - 1, i + 2) {
            if (U[y][x] == '#') {
              count++;
            }
          }
        }
        cout << count;
      } else {
        cout << "#";
      }

      count = 0;
    }
    cout << endl;
  }

  rep(i, 0, A) {
    // cout << U[i] << endl;;
  }

  // cin >> S;

  // a = (ll *)malloc(M * sizeof(ll));
  // b = (ll *)malloc(M * sizeof(ll));
  // vector<pair<int,int>> ch(N+1);
  // vector<bool> flag(N);

  // count--;
  //	cout << X << endl;
  // x = (ll *)malloc((N+1) * sizeof(ll));
  // c = (ll *)malloc((N) * sizeof(ll));
  // cin >> D >> G;
  // vector<pair<int,int>> tr(M);
  // vector<pair<int,int>> qes(Q);
  // sort(c.begin(), c.end());

  // cout << c[2] - c[1] << endl;

  /*A = N / 1000;
  N %= 1000;
  B = N / 100;
  N %= 100;
  C = N / 10;
  N %= 10;
  D = N;*/

  return 0;
}

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

  return b;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <time.h>
#include <vector>
// #include "bits/stdc++.h"
using namespace std;
// using namespace std::vector;
#define rep(i, a, b) for (int i = (a); i < (b); ++i)

#define huge 1000000007
typedef long long ll;
ll gcd(ll a, ll b);

int main(void) {

  ll N = 0;
  ll A = 0;
  ll B = 0;
  ll C = 0;
  ll D = 0;
  ll M = 0;
  // ll M = 0;
  ll a[200] = {};
  ll b[200] = {};
  ll v[3] = {};
  // ll *a, *b, *x;
  string S, T, U[52];
  ll count = 0;
  ll sum = 0;
  ll min = 100000;
  ll max = 0;
  int flag = 0;
  char temp, head;
  // int is = 0;

  vector<int> c(3);
  // char p;
  cin >> A >> B;

  rep(i, 0, A) { cin >> U[i]; }

  rep(j, 0, A) {
    rep(i, 0, B) {

      if (U[j][i] == '.') {

        rep(y, j - 1, j + 2) {
          rep(x, i - 1, i + 2) {
            if (y >= 0 && x >= 0)
              if (U[y][x] == '#') {
                count++;
              }
          }
        }
        cout << count;
      } else {
        cout << "#";
      }

      count = 0;
    }
    cout << endl;
  }

  rep(i, 0, A) {
    // cout << U[i] << endl;;
  }

  // cin >> S;

  // a = (ll *)malloc(M * sizeof(ll));
  // b = (ll *)malloc(M * sizeof(ll));
  // vector<pair<int,int>> ch(N+1);
  // vector<bool> flag(N);

  // count--;
  //	cout << X << endl;
  // x = (ll *)malloc((N+1) * sizeof(ll));
  // c = (ll *)malloc((N) * sizeof(ll));
  // cin >> D >> G;
  // vector<pair<int,int>> tr(M);
  // vector<pair<int,int>> qes(Q);
  // sort(c.begin(), c.end());

  // cout << c[2] - c[1] << endl;

  /*A = N / 1000;
  N %= 1000;
  B = N / 100;
  N %= 100;
  C = N / 10;
  N %= 10;
  D = N;*/

  return 0;
}

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

  return b;
}