#include <bits/stdc++.h>
int main(void) {
  int i, j;
  int x, y;
  int m, n;
  int k;
  static char map[1010][1010];
  scanf("%d %d", &m, &n);
  scanf("%d", &k);
  for (i = 0; i < m; i++) {
    scanf("%s", map + i);
  }
  for (i = 0; i < k; i++) {
    int a, b, c, d;
    int Jcount = 0;
    int Ocount = 0;
    int Icount = 0;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    for (y = a - 1; y < c; y++) {
      for (x = b - 1; x < d; x++) {
        if (map[y][x] == 'J') Jcount++;
        if (map[y][x] == 'O') Ocount++;
        if (map[y][x] == 'I') Icount++;
      }
    }
    printf("%d %d %d\n", Jcount, Ocount, Icount);
  }
  return (0);
}
