#include <bits/stdc++.h>
using namespace std;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
int main() {
  int m, n;
  scanf("%d%d", &m, &n);
  int k;
  scanf("%d", &k);
  char field[1024][1024];
  for (int i = 0; i < (m); i++) {
    scanf("%s", field[i]);
  }
  while (k--) {
    int y1, x1, y2, x2;
    int J = 0, O = 0, I = 0;
    scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
    for (int i = y1 - 1; i < y2; i++) {
      for (int j = x1 - 1; j < x2; j++) {
        if (field[i][j] == 'J')
          J++;
        else if (field[i][j] == 'O')
          O++;
        else
          I++;
      }
    }
    printf("%d %d %d\n", J, O, I);
  }
  return 0;
}
