#include <bits/stdc++.h>
int main() {
  int w, h, n, i, j, k, a, b, c, d;
  int m[1001][1001][3] = {0};
  char s;
  scanf("%d %d %d\n", &h, &w, &n);
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w + 1; j++) {
      scanf("%c", &s);
      if (s == 10) continue;
      printf("%c", s);
      for (k = 0; k < 3; k++) {
        m[i][j][k] += m[i - 1][j][k];
        m[i][j][k] += m[i][j - 1][k];
        m[i][j][k] -= m[i - 1][j - 1][k];
      }
      if (s == 'J') m[i][j][0]++;
      if (s == 'O') m[i][j][1]++;
      if (s == 'I') m[i][j][2]++;
    }
    printf("\n");
  }
  for (i = 0; i < n; i++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    for (j = 0; j < 3; j++) {
      if (j) printf(" ");
      printf("%d",
             m[c][d][j] - m[c][b - 1][j] - m[a - 1][d][j] + m[a - 1][b - 1][j]);
    }
    printf("\n");
  }
  return 0;
}
