#include<bits/stdc++.h>
using namespace std;
char area[1001][1001]; // 調査対象の領域
int rui[3][1001][1001]; // 累積和
int main(){
  int h,w,k,a,b,c,d;
  scanf("%d %d %d",&h,&w,&k);
  for(int i=1;i<=h;i++){
    for(int j=1;j<=w;j++){
      scanf(" %c",&area[i][j]);
      rui[0][i][j] = (area[i][j] == 'J');
      rui[1][i][j] = (area[i][j] == 'O');
      rui[2][i][j] = (area[i][j] == 'I');
      if(i != 1){
	rui[0][i][j] += rui[0][i-1][j];
	rui[1][i][j] += rui[1][i-1][j];
	rui[2][i][j] += rui[2][i-1][j];
      }
      if(j != 1){
	rui[0][i][j] += rui[0][i][j-1];
	rui[1][i][j] += rui[1][i][j-1];
	rui[2][i][j] += rui[2][i][j-1];
      }
      if(i != 1 && j != 1){
	rui[0][i][j] -= rui[0][i-1][j-1];
	rui[1][i][j] -= rui[1][i-1][j-1];
	rui[2][i][j] -= rui[2][i-1][j-1];
      }
    }
  }
  int ans;
  for(int i=0;i<k;i++){
    scanf("%d %d %d %d",&a,&b,&c,&d);
    for(int j=0;j<3;j++){
      ans = 0;
      ans = rui[j][c][d];
      if(a != 1) ans -= rui[j][a-1][d];
      if(b != 1) ans -= rui[j][c][b-1];
      if(a != 1 && b != 1) ans += rui[j][a-1][b-1];
      printf("%d",ans);
      putchar(j==2?'\n':' ');
    }
  }
  return (0);
}