#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int M, N, K;
  char map[1010][1010];
  int jmap[1010][1010];
  int omap[1010][1010];
  int imap[1010][1010];
  scanf("%d %d %d", &M, &N, &K);
  for (int i = 0; i < M; i++) {
    scanf("%s", map[i]);
  }
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      jmap[i][j] = 0;
      omap[i][j] = 0;
      imap[i][j] = 0;
    }
  }
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      jmap[i][j] = jmap[i - 1][j] + jmap[i][j - 1] - jmap[i - 1][j - 1];
      omap[i][j] = omap[i - 1][j] + omap[i][j - 1] - omap[i - 1][j - 1];
      imap[i][j] = imap[i - 1][j] + imap[i][j - 1] - imap[i - 1][j - 1];
      if (map[i - 1][j - 1] == 'J')
        jmap[i][j]++;
      else if (map[i - 1][j - 1] == 'O')
        omap[i][j]++;
      else if (map[i - 1][j - 1] == 'I')
        imap[i][j]++;
    }
  }
  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    int jans =
        jmap[c][d] - jmap[c][b - 1] - jmap[a - 1][d] + jmap[a - 1][b - 1];
    int oans =
        omap[c][d] - omap[c][b - 1] - omap[a - 1][d] + omap[a - 1][b - 1];
    int ians =
        imap[c][d] - imap[c][b - 1] - imap[a - 1][d] + imap[a - 1][b - 1];
    printf("%d %d %d\n", jans, oans, ians);
  }
  return 0;
}
