
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

string tt[1000];

int main(){
  int n, m, k;
  cin >> m >> n;
  cin >> k;
  for(int i = 0; i < m; i++){
    cin >> tt[i];
  }
  for(int i = 0; i < k; i++){
    int x1, y1, x2, y2, j = 0, o = 0, i = 0;
    cin >> x1 >> y1 >> x2 >> y2;
    for(int x = x1; x <= x2; x++){
      for(int y = y1; y <= y2; y++){
        char c = tt[x - 1][y - 1];
        //printf("(%d:%d:%c)", x, y, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }
    printf("%d %d %d\n", j, o, i);
  }
  return 0;
}