#include <bits/stdc++.h>
using namespace std;
char c[1000][1000];
int d[1000][1000][3] = {{{0}, {0}}};
const int J = 0;
const int O = 1;
const int I = 2;
int main(void) {
  int m, n, k;
  cin >> m >> n >> k;
  for (int i = 0; i < m; i++) {
    scanf("%s", c[i]);
  }
  for (int y = 0; y < m; y++) {
    for (int x = 0; x < n; x++) {
      char t = c[y][x];
      if (!x && !y) {
      } else if (!x) {
        d[y][x][J] = d[y - 1][x][J];
        d[y][x][O] = d[y - 1][x][O];
        d[y][x][I] = d[y - 1][x][I];
      } else if (!y) {
        d[y][x][J] = d[y][x - 1][J];
        d[y][x][O] = d[y][x - 1][O];
        d[y][x][I] = d[y][x - 1][I];
      } else {
        d[y][x][J] = d[y - 1][x][J] + d[y][x - 1][J] - d[y - 1][x - 1][J];
        d[y][x][O] = d[y - 1][x][O] + d[y][x - 1][O] - d[y - 1][x - 1][O];
        d[y][x][I] = d[y - 1][x][I] + d[y][x - 1][I] - d[y - 1][x - 1][I];
      }
      if (t == 'J')
        d[y][x][J]++;
      else if (t == 'O')
        d[y][x][O]++;
      else
        d[y][x][I]++;
    }
  }
  for (int i = 0; i < k; i++) {
    int y1, x1, y2, x2;
    cin >> y1 >> x1 >> y2 >> x2;
    y1--;
    y1--;
    x1--;
    x1--;
    y2--;
    x2--;
    if (y1 < 0) y1 = 0;
    if (x1 < 0) x1 = 0;
    int ans[3] = {0};
    ans[J] = d[y2][x2][J] - d[y1][x2][J] - d[y2][x1][J] + d[y1][x1][J];
    ans[O] = d[y2][x2][O] - d[y1][x2][O] - d[y2][x1][O] + d[y1][x1][O];
    ans[I] = d[y2][x2][I] - d[y1][x2][I] - d[y2][x1][I] + d[y1][x1][I];
    printf("%d %d %d\n", ans[J], ans[O], ans[I]);
  }
  return 0;
}
