#include <bits/stdc++.h>
int main() {
  int m, n;
  int k;
  int i, j, l;
  scanf("%d %d", &m, &n);
  scanf("%d", &k);
  char** list = (char**)malloc(sizeof(char*) * (m));
  for (i = 0; i < m; i++) {
    list[i] = (char*)malloc(sizeof(char) * (n + 1));
    scanf("%s", list[i]);
  }
  for (i = 0; i < k; i++) {
    int a, b, c, d;
    int count[2] = {0, 0};
    scanf("%d %d %d %d", &a, &b, &c, &d);
    for (j = a - 1; j < c; j++) {
      for (l = b - 1; l < d; l++) {
        if (list[j][l] == 'J')
          count[0]++;
        else if (list[j][l] == 'O')
          count[1]++;
      }
    }
    printf("%d %d %d\n", count[0], count[1],
           (c - a + 1) * (d - b + 1) - count[0] - count[1]);
  }
  return 0;
}
