#include <bits/stdc++.h>
int M, N, K;
int Js[1010][1010];
int Os[1010][1010];
int Is[1010][1010];
using namespace std;
int main(void) {
  scanf("%d %d\n", &M, &N);
  scanf("%d\n", &K);
  char c;
  for (int j = 1; j <= M; ++j) {
    for (int i = 1; i <= N; ++i) {
      scanf("%c", &c);
      if (c == 'J') {
        Js[j][i] = Js[j][i - 1] + 1;
        Os[j][i] = Os[j][i - 1];
        Is[j][i] = Is[j][i - 1];
      }
      if (c == 'O') {
        Os[j][i] = Os[j][i - 1] + 1;
        Js[j][i] = Js[j][i - 1];
        Is[j][i] = Is[j][i - 1];
      }
      if (c == 'I') {
        Is[j][i] = Is[j][i - 1] + 1;
        Js[j][i] = Js[j][i - 1];
        Os[j][i] = Os[j][i - 1];
      }
    }
    getchar();
  }
  for (int i = 0; i < K; ++i) {
    int n, w, s, e, jc = 0, oc = 0, ic = 0;
    scanf("%d %d %d %d", &n, &w, &s, &e);
    for (int j = n; j <= s; ++j) {
      jc += Js[j][e] - Js[j][w - 1];
      oc += Os[j][e] - Os[j][w - 1];
      ic += Is[j][e] - Is[j][w - 1];
    }
    printf("%d %d %d", jc, oc, ic);
  }
  return 0;
}
