#include <bits/stdc++.h>
using namespace std;
char c[1010][1010];
int J[1010][1010];
int O[1010][1010];
int I[1010][1010];
int main() {
  int m, n, k;
  scanf("%d %d", &m, &n);
  scanf("%d", &k);
  for (int i = 0; i < m; i++) {
    scanf("%s", c[i]);
  }
  for (int i = 0; i < 1010; i++) {
    for (int j = 0; j < 1010; j++) {
      J[i][j] = 0;
      O[i][j] = 0;
      I[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      J[i][j] = J[i - 1][j] + J[i][j - 1] - J[i - 1][j - 1];
      O[i][j] = O[i - 1][j] + O[i][j - 1] - O[i - 1][j - 1];
      I[i][j] = I[i - 1][j] + I[i][j - 1] - I[i - 1][j - 1];
      if (c[i - 1][j - 1] == 'J') {
        J[i][j]++;
      }
      if (c[i - 1][j - 1] == 'O') {
        O[i][j]++;
      }
      if (c[i - 1][j - 1] == 'I') {
        I[i][j]++;
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a0, a1, a2, a3;
    scanf("%d%d%d%d", &a0, &a1, &a2, &a3);
    int r0 = J[a2][a3] - J[a2][a1 - 1] - J[a0 - 1][a3] + J[a0 - 1][a1 - 1];
    int r1 = O[a2][a3] - O[a2][a1 - 1] - O[a0 - 1][a3] + O[a0 - 1][a1 - 1];
    int r2 = I[a2][a3] - I[a2][a1 - 1] - I[a0 - 1][a3] + I[a0 - 1][a1 - 1];
    printf("%d %d %d\n", r0, r1, r2);
  }
  return 0;
}
