#include <bits/stdc++.h>
using std::min;
const int N = 100;
char m[1024][1024];
int RMQ[1000 / N + 10][1000 / N + 10][3];
int h, w;
void F(int a, int b, int c, int d, int& J, int& O, int& I) {
  for (int y = a; y < c; ++y)
    for (int x = b; x < d; ++x)
      if (m[y][x] == 'J')
        ++J;
      else if (m[y][x] == 'O')
        ++O;
      else
        ++I;
}
int main() {
  int x, y, x2, y2, k, a, b, c, d, J, O, I;
  scanf("%d%d%d", &h, &w, &k);
  for (y = 0; y < h; ++y) scanf("%s", m[y]);
  for (y = 0; y + N < h; y += N)
    for (x = 0; x + N < w; x += N)
      F(y, x, y + N, x + N, RMQ[y][x][0], RMQ[y][x][1], RMQ[y][x][2]);
  while (k--) {
    J = O = I = 0;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    --a, --b;
    for (y = a, y2 = min(a / N * N + N, c); y < c; y = y2, y2 = min(y2 + N, c))
      for (x = b, x2 = min(b / N * N + N, d); x < d;
           x = x2, x2 = min(x2 + N, d)) {
        if (y % N == 0 && y2 % N == 0 && x % N == 0 && x2 % N == 0)
          J += RMQ[y][x][0], O += RMQ[y][x][1], I += RMQ[y][x][2];
        else
          F(y, x, y2, x2, J, O, I);
      }
    printf("%d %d %d\n", J, O, I);
  }
  return 0;
}
