#include <bits/stdc++.h>
int main(void) {
  int i, j, M, N, k, s1, s2, s3, s4, J[1000][1000], O[1000][1000],
      I[1000][1000], x, y, z;
  char m[1000][1000], a[1000];
  scanf("%d%d", &M, &N);
  scanf("%d", &k);
  for (i = 0; i <= M - 1; i++) {
    scanf("%s", a);
    for (j = 0; j <= N - 1; j++) {
      m[i][j] = a[j];
    }
  }
  for (i = 0; i <= M - 1; i++) {
    J[i][0] = 0;
    O[i][0] = 0;
    I[i][0] = 0;
    for (j = 0; j <= N - 1; j++) {
      if (m[i][j] == 'J') {
        J[i][j]++;
      } else if (m[i][j] == 'O') {
        O[i][j]++;
      } else {
        I[i][j]++;
      }
      J[i][j + 1] = J[i][j];
      O[i][j + 1] = O[i][j];
      I[i][j + 1] = I[i][j];
    }
  }
  for (i = 0; i <= k - 1; i++) {
    x = 0;
    y = 0;
    z = 0;
    scanf("%d%d%d%d", &s1, &s2, &s3, &s4);
    for (i = s1 - 1; i <= s3 - 1; i++) {
      if (s2 >= 2) {
        x += J[i][s4 - 1] - J[i][s2 - 2];
        y += O[i][s4 - 1] - O[i][s2 - 2];
        z += I[i][s4 - 1] - I[i][s2 - 2];
      } else {
        x += J[i][s4 - 1];
        y += O[i][s4 - 1];
        z += I[i][s4 - 1];
      }
    }
    printf("%d %d %d\n", x, y, z);
  }
  return 0;
}
