#include <bits/stdc++.h>
using namespace std;
const int INF = 1001001001;
int M, N, K;
char map[1002][1002];
int rmap[1002][1002][3];
int main() {
  scanf("%d %d", &M, &N);
  scanf("%d", &K);
  for (int i = 0; i < 3; i++) {
    rmap[0][0][i] = 0;
  }
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      char a;
      scanf("%c", &a);
      if (a == 10) {
        scanf("%c", &a);
      }
      int s = 0;
      map[i][j] = a;
      if (map[i][j] == 'J')
        s = 0;
      else if (map[i][j] == 'O')
        s = 1;
      else
        s = 2;
      for (int k = 0; k < 3; k++) {
        int p = (int)(s == k);
        if (i == 0 && j == 0) {
          rmap[i][j][k] += p;
        } else if (i == 0) {
          rmap[i][j][k] += rmap[i][j - 1][k] + p;
        } else if (j == 0) {
          rmap[i][j][k] += rmap[i - 1][j][k] + p;
        } else {
          rmap[i][j][k] +=
              rmap[i][j - 1][k] + rmap[i - 1][j][k] - rmap[i - 1][j - 1][k] + p;
        }
      }
    }
  }
  for (int i = 0; i < K; i++) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    a--;
    b--;
    c--;
    d--;
    printf("\n");
    for (int j = 0; j < 3; j++) {
      int ans = rmap[c][d][j];
      if (a > 0) ans -= rmap[a - 1][d][j];
      if (b > 0) ans -= rmap[c][b - 1][j];
      if (a > 0 && b > 0) ans += rmap[a - 1][b - 1][j];
      printf("%d ", ans);
    }
  }
}
