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

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W, K, sx, sy, tx, ty;

  cin >> H >> W >> K;

  vector<string> M(H);

  for (int i = 0; i < H; i++) {
    cin >> M[i];
  }

  vector<vector<int>> J(H, vector<int>(W, 0));
  vector<vector<int>> O(H, vector<int>(W, 0));
  vector<vector<int>> I(H, vector<int>(W, 0));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == 'J') {
        J[i][j] = 1;
      }

      if (M[i][j] == 'O') {
        O[i][j] = 1;
      }

      if (M[i][j] == 'I') {
        I[i][j] = 1;
      }

      if (i != 0) {
        J[i][j] += J[i - 1][j];
        O[i][j] += O[i - 1][j];
        I[i][j] += I[i - 1][j];
      }

      if (j != 0) {
        J[i][j] += J[i][j - 1];
        O[i][j] += O[i][j - 1];
        I[i][j] += I[i][j - 1];
      }

      if (i != 0 && j != 0) {
        J[i][j] -= J[i - 1][j - 1];
        O[i][j] -= O[i - 1][j - 1];
        I[i][j] -= I[i - 1][j - 1];
      }
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> sy >> sx >> ty >> tx;

    int J_, O_, I_;

    J_ = J[ty - 1][tx - 1];
    O_ = O[ty - 1][tx - 1];
    I_ = I[ty - 1][tx - 1];

    if (sx != 1 && sy != 1) {
      J_ += J[sy - 2][sx - 2];
      O_ += O[sy - 2][sx - 2];
      I_ += I[sy - 2][sx - 2];
    }

    if (sx != 1) {
      J_ -= J[ty - 1][sx - 2];
      O_ -= O[ty - 1][sx - 2];
      I_ -= I[ty - 1][sx - 2];
    }

    if (sy != 1) {
      J_ -= J[sy - 2][tx - 1];
      O_ -= O[sy - 2][tx - 1];
      I_ -= I[sy - 2][tx - 1];
    }

    cout << J_ << ' ' << O_ << ' ' << I_ << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {
  int H, W, K, sx, sy, tx, ty;

  cin >> H >> W >> K;

  vector<string> M(H);

  for (int i = 0; i < H; i++) {
    cin >> M[i];
  }

  vector<vector<int>> J(H, vector<int>(W, 0));
  vector<vector<int>> O(H, vector<int>(W, 0));
  vector<vector<int>> I(H, vector<int>(W, 0));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (M[i][j] == 'J') {
        J[i][j] = 1;
      }

      if (M[i][j] == 'O') {
        O[i][j] = 1;
      }

      if (M[i][j] == 'I') {
        I[i][j] = 1;
      }

      if (i != 0) {
        J[i][j] += J[i - 1][j];
        O[i][j] += O[i - 1][j];
        I[i][j] += I[i - 1][j];
      }

      if (j != 0) {
        J[i][j] += J[i][j - 1];
        O[i][j] += O[i][j - 1];
        I[i][j] += I[i][j - 1];
      }

      if (i != 0 && j != 0) {
        J[i][j] -= J[i - 1][j - 1];
        O[i][j] -= O[i - 1][j - 1];
        I[i][j] -= I[i - 1][j - 1];
      }
    }
  }

  for (int i = 0; i < K; i++) {
    cin >> sy >> sx >> ty >> tx;

    int J_, O_, I_;

    J_ = J[ty - 1][tx - 1];
    O_ = O[ty - 1][tx - 1];
    I_ = I[ty - 1][tx - 1];

    if (sx != 0 && sy != 0) {
      J_ += J[sy - 2][sx - 2];
      O_ += O[sy - 2][sx - 2];
      I_ += I[sy - 2][sx - 2];
    }

    if (sx != 0) {
      J_ -= J[ty - 1][sx - 2];
      O_ -= O[ty - 1][sx - 2];
      I_ -= I[ty - 1][sx - 2];
    }

    if (sy != 0) {
      J_ -= J[sy - 2][tx - 1];
      O_ -= O[sy - 2][tx - 1];
      I_ -= I[sy - 2][tx - 1];
    }

    cout << J_ << ' ' << O_ << ' ' << I_ << endl;
  }

  return 0;
}