#include<iostream>
using namespace std;
int J[1000][1000]={0};
int O[1000][1000]={0};
int I[1000][1000]={0};
char maps[1000][1000];
int main(){
  int m,n,k;
  int a,b,c,d;
  cin>>m>>n>>k;
  for(int i=1;i<=m;i++){
    for(int j=1;j<=n;j++){
      cin>>maps[i][j];
      J[i][j]=J[i][j-1];
      O[i][j]=O[i][j-1];
      I[i][j]=I[i][j-1];
      if(maps[i][j]=='J'){
	J[i][j]=J[i][j]+1;
      }
      else if(maps[i][j]=='O'){
	O[i][j]=O[i][j]+1;
      }
      else{
	I[i][j]=I[i][j]+1;
      }
    }
  }
  for(int i=0;i<k;i++){
    cin>>a>>b>>c>>d;
    int jcount=0;
    int ocount=0;
    int icount=0;
    for(int j=a;j<=c;j++){
      jcount=jcount+J[j][d]-J[j][b];
      ocount=ocount+O[j][d]-O[j][b];
      icount=icount+I[j][d]-I[j][b];
      if(maps[j][b]=='J'){
	jcount++;
      }
      else if(maps[j][b]=='O'){
	ocount++;
      }
      else{
	icount++;
      }
    }
    cout<<jcount<<" "<<ocount<<" "<<icount<<endl;
  }
}