#include <bits/stdc++.h>
using namespace std;
int i, j, m, n, k, Jungle, Ocean, Ice, c, nw[2], se[2];
char str[1000][1000];
int sort(const void *a, const void *b) { return *(int *)a - *(int *)b; }
int main() {
  scanf("%d %d", &m, &n);
  scanf("%d", &c);
  for (i = 0; i < m; i++)
    for (j = 0; j < n; j++) scanf("%1s", &str[i][j]);
  for (i = 0; i < c; i++) {
    Jungle = 0;
    Ocean = 0;
    Ice = 0;
    scanf("%d %d %d %d", &nw[0], &nw[1], &se[0], &se[1]);
    for (j = nw[0] - 1; j < se[0]; j++) {
      for (k = nw[1] - 1; k < se[1]; k++) {
        if (str[j][k] == 'J') Jungle++;
        if (str[j][k] == 'O') Ocean++;
        if (str[j][k] == 'I') Ice++;
      }
    }
    printf("%d %d %d\n", Jungle, Ocean, Ice);
  }
  return 0;
}
