#include <bits/stdc++.h>
using namespace std;
int main(void) {
  char map[1001][1001];
  int a, b, c, m, n, k;
  cin >> m >> n;
  cin >> k;
  for (a = 0; a < m; a++) {
    cin >> map[a];
  }
  for (c = 0; c < k; c++) {
    int ax, ay, bx, by;
    int ja = 0, oa = 0, ia = 0;
    cin >> ay >> ax >> by >> bx;
    for (a = ay - 1; a < by; a++) {
      for (b = ax - 1; b < bx; b++) {
        if (map[a][b] == 'J') ja++;
        if (map[a][b] == 'O') oa++;
        if (map[a][b] == 'I') ia++;
      }
    }
    cout << ja << " " << oa << " " << ia << endl;
  }
  return 0;
}
