Problem p00483 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int M, N, K, a, b, c, d, ansJ, ansO;
  char input[1003];
  int sumJ[1003][1003], sumO[1003][1003];

  cin >> M >> N >> K;

  REP(j, M + 1) {
    REP(k, N + 1) {
      sumJ[j][k] = 0;
      sumO[j][k] = 0;
    }
  }

  for (int j = 1; j <= M; j++) {
    cin >> input;
    for (int k = 1; k <= N; k++) {
      if (input[k - 1] == 'J')
        sumJ[j][k] = 1;
      if (input[k - 1] == 'O')
        sumO[j][k] = 1;
      // if (input[k-1]=='I') sum[j][k][2] = 1;
    }
  }
  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }
  cout << endl;*/

  for (int j = 1; j <= M; j++) {
    for (int k = 1; k <= N; k++) {
      /*REP(l,3){
              sum[j][k][l] += sum[j][k-1][l];
      }*/
      sumJ[j][k] += sumJ[j][k - 1];
      sumO[j][k] += sumO[j][k - 1];
    }
  }
  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }
  cout << endl;*/

  for (int k = 1; k <= N; k++) {
    for (int j = 1; j <= M; j++) {
      /*REP(l,3){
              sum[j][k][l] += sum[j-1][k][l];
      }*/
      sumJ[j][k] += sumJ[j - 1][k];
      sumO[j][k] += sumO[j - 1][k];
    }
  }

  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }*/

  REP(i, K) {
    cin >> a >> b >> c >> d;
    // REP(l,3) ans[l] =
    // sum[a-1][b-1][l]+sum[c][d][l]-sum[a-1][d][l]-sum[c][b-1][l];
    ansJ = sumJ[a - 1][b - 1] + sumJ[c][d] - sumJ[a - 1][d] - sumJ[c][b - 1];
    ansO = sumO[a - 1][b - 1] + sumO[c][d] - sumO[a - 1][d] - sumO[c][b - 1];
    printf(
        "%d %d %d\\n", ansJ, ansO,
        (c * d + (a - 1) * (b - 1) - c * (b - 1) - d * (a - 1) - ansJ - ansO));
  }

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

#define REP(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int M, N, K, a, b, c, d, ans[5];
  string input;
  vector<vector<vector<int>>> sum;
  vector<vector<int>> po;
  vector<int> z;

  REP(l, 3) z.push_back(0);

  cin >> M >> N >> K;
  REP(j, M + 1) {
    REP(k, N + 1) { po.push_back(z); }
    sum.push_back(po);
  }

  for (int j = 1; j <= M; j++) {
    cin >> input;
    for (int k = 1; k <= N; k++) {
      if (input[k - 1] == 'J')
        sum[j][k][0] = 1;
      if (input[k - 1] == 'O')
        sum[j][k][1] = 1;
      if (input[k - 1] == 'I')
        sum[j][k][2] = 1;
    }
  }
  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }
  cout << endl;*/

  for (int j = 1; j <= M; j++) {
    for (int k = 1; k <= N; k++) {
      REP(l, 3) { sum[j][k][l] += sum[j][k - 1][l]; }
    }
  }
  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }
  cout << endl;*/

  for (int k = 1; k <= N; k++) {
    for (int j = 1; j <= M; j++) {
      REP(l, 3) { sum[j][k][l] += sum[j - 1][k][l]; }
    }
  }

  /*REP(i,M+1){
          REP(j,N+1){
                  cout << "(";
                  REP(l,3) printf("%d,", sum[i][j][l]);
                  cout << ") ";
          }
          cout << endl;
  }*/

  REP(i, K) {
    cin >> a >> b >> c >> d;
    REP(l, 3)
    ans[l] = sum[a - 1][b - 1][l] + sum[c][d][l] - sum[a - 1][d][l] -
             sum[c][b - 1][l];
    printf("%d %d %d\\n", ans[0], ans[1], ans[2]);
  }

  return 0;
}