#include <bits/stdc++.h>
using namespace std;
int main() {
  int h, w;
  while (scanf("%d%d", &h, &w) != EOF) {
    int k;
    scanf("%d", &k);
    static short int J[1000][1000], O[1000][1000], I[1000][1000];
    static short int size_j[1000], size_o[1000], size_i[1000];
    for (int i = 0; i < h; i++) {
      size_j[i] = size_o[i] = size_i[i] = 0;
      char str[1001];
      scanf("%s", str);
      for (int j = 0; j < w; j++) {
        if (str[j] == 'J')
          J[i][size_j[i]++] = j;
        else if (str[j] == 'O')
          O[i][size_o[i]++] = j;
        else if (str[j] == 'I')
          I[i][size_i[i]++] = j;
      }
    }
    int y1, x1, y2, x2;
    for (int i = 0; i < k; i++) {
      int ans_j = 0, ans_o = 0, ans_i = 0;
      scanf("%d%d%d%d", &y1, &x1, &y2, &x2);
      y1--;
      x1--;
      y2--;
      x2--;
      for (int j = y1; j <= y2; j++) {
        ans_j += upper_bound(J[j], J[j] + size_j[j], x2) -
                 lower_bound(J[j], J[j] + size_j[j], x1);
        ans_o += upper_bound(O[j], O[j] + size_o[j], x2) -
                 lower_bound(O[j], O[j] + size_o[j], x1);
        ans_i += upper_bound(I[j], I[j] + size_i[j], x2) -
                 lower_bound(I[j], I[j] + size_i[j], x1);
      }
      printf("%d %d %d\n", ans_j, ans_o, ans_i);
    }
  }
  return 0;
}
