#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int M, N, K, x1, y1, x2, y2, cJ, cO, cI;
  char maps[1000][1000] = {0};
  cin >> M >> N;
  cin >> K;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++) {
      cin >> maps[i][j];
    }
  }
  for (int i = 0; i < K; i++) {
    cJ = 0, cO = 0, cI = 0;
    cin >> x1 >> y1 >> x2 >> y2;
    for (int j = (x1 - 1); j < x2; j++) {
      for (int k = (y1 - 1); k < y2; k++) {
        if (maps[j][k] == 'J') {
          cJ++;
        }
        if (maps[j][k] == 'O') {
          cO++;
        }
        if (maps[j][k] == 'I') {
          cI++;
        }
      }
    }
    cout << cJ << ' ' << cO << ' ' << cI << endl;
  }
  return 0;
}
