ostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main(void) {
  int M, N, K;
  char res[1000][1000];
  int xp, yp, xq, yq;
  int J, O, I;

  cin >> M >> N;
  cin >> K;
  for(int i = 0 ; i < M ; i++) {
    scanf("%s", res[i]);
  }
  
  for(int a = 0 ; a < K ; a++) {
    cin >> xp >> yp >> xq >> yq;
    //while(cin >> xp >> yp >> xq >> yq) {
    J = 0; O = 0; I = 0;

    for(int i = xp-1 ; i < xq ; i++) {
      for(int j = yp-1 ; j < yq ; j++) {
	if(res[i][j] == 'J') J++;
	if(res[i][j] == 'O') O++;
        if(res[i][j] == 'I') I++;
	//cout << res[i][j] << ' ';
      }
      //cout << endl;
    }
    printf("%d %d %d\n", J, O, I);
  }
  return 0;
}