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

#include <iostream>
using namespace std;

inline int check(char c, int k) {
  if (k == 0 && c == 'J')
    return 1;
  if (k == 1 && c == 'O')
    return 1;
  if (k == 2 && c == 'I')
    return 1;

  return 0;
}

const int R = 1010;
const int C = 1010;

int main() {
  int r, c;

  while (cin >> r >> c) {
    if (r == c && r == 0)
      break;

    int q;
    cin >> q;
    char A[r][c];

    for (int i = 0; i < r; ++i) {
      for (int j = 0; j < c; ++j) {
        cin >> A[i][j];
      }
    }

    static int r_sum[3][R][C];
    static int grid_sum[3][R][C];

    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j < R; ++j) {
        for (int k = 0; k < C; ++k) {
          r_sum[i][j][k] = 0;
          grid_sum[i][j][k] = 0;
        }
      }
    }

    for (int k = 0; k < 3; ++k) {
      for (int i = 0; i < r; ++i) {
        r_sum[k][i][0] = 0;
        for (int j = 1; j <= c; ++j) {
          r_sum[k][i][j] += r_sum[k][i][j - 1] + check(A[i][j - 1], k);
        }
      }
    }

    for (int k = 0; k < 3; ++k) {
      for (int i = 0; i <= r; ++i) {
        for (int j = 0; j <= c; ++j) {
          grid_sum[k][i][j] = 0;
          for (int l = 0; l < i; ++l) {
            grid_sum[k][i][j] += r_sum[k][l][j];
          }
        }
      }
    }

    // cout << r_sum[0][0][2] << endl;

    for (int x = 0; x < q; ++x) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      --a;
      --b;
      int res[3];
      for (int i = 0; i < 3; ++i) {
        res[i] = grid_sum[i][c][d] - grid_sum[i][a][d] - grid_sum[i][c][b] +
                 grid_sum[i][a][b];
      }
      cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

inline int check(char c, int k) {
  if (k == 0 && c == 'J')
    return 1;
  if (k == 1 && c == 'O')
    return 1;
  if (k == 2 && c == 'I')
    return 1;

  return 0;
}

int main() {
  int r, c;

  while (cin >> r >> c) {
    if (r == c && r == 0)
      break;

    int q;
    cin >> q;
    char A[r][c];

    for (int i = 0; i < r; ++i) {
      for (int j = 0; j < c; ++j) {
        cin >> A[i][j];
      }
    }

    int r_sum[3][r][c + 1];
    int grid_sum[3][r + 1][c + 1];

    for (int i = 0; i < 3; ++i) {
      for (int j = 0; j <= r; ++j) {
        for (int k = 0; k <= c; ++k) {
          if (j < r) {
            r_sum[i][j][k] = 0;
          }
          grid_sum[i][j][k] = 0;
        }
      }
    }

    for (int k = 0; k < 3; ++k) {
      for (int i = 0; i < r; ++i) {
        r_sum[k][i][0] = 0;
        for (int j = 1; j <= c; ++j) {
          r_sum[k][i][j] += r_sum[k][i][j - 1] + check(A[i][j - 1], k);
        }
      }
    }
    // cout << check(A[0][0],0) << endl;

    for (int k = 0; k < 3; ++k) {
      for (int i = 0; i <= r; ++i) {
        for (int j = 0; j <= c; ++j) {
          grid_sum[k][i][j] = 0;
          for (int l = 0; l < i; ++l) {
            grid_sum[k][i][j] += r_sum[k][l][j];
          }
        }
      }
    }

    // cout << r_sum[0][0][2] << endl;

    for (int x = 0; x < q; ++x) {
      int a, b, c, d;
      cin >> a >> b >> c >> d;
      --a;
      --b;
      int res[3];
      for (int i = 0; i < 3; ++i) {
        res[i] = grid_sum[i][c][d] - grid_sum[i][a][d] - grid_sum[i][c][b] +
                 grid_sum[i][a][b];
      }
      cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
    }
  }
  return 0;
}