#include <bits/stdc++.h>
using namespace std;
char MAP[1000][1000];
void solve(int LU_x, int LU_y, int RD_x, int RD_y);
void refresh(void);
int main(void) {
  refresh();
  int M, N, K;
  cin >> M >> N;
  cin >> K;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> MAP[i][j];
    }
  }
  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    solve(a, b, c, d);
  }
}
void solve(int LU_x, int LU_y, int RD_x, int RD_y) {
  int J = 0, O = 0, I = 0;
  for (int i = LU_y - 1; i < RD_y; i++) {
    for (int j = LU_x - 1; j < RD_x; j++) {
      if (MAP[j][i] == 'J') {
        J++;
      } else if (MAP[j][i] == 'O') {
        O++;
      } else if (MAP[j][i] == 'I') {
        I++;
      }
    }
  }
  cout << J << ' ' << O << ' ' << I << endl;
}
void refresh(void) {
  for (int i = 0; i < 1000; i++) {
    for (int j = 0; j < 1000; j++) {
      MAP[i][j] = 0;
    }
  }
}
