#include<stdio.h>
#include<math.h>
#include<algorithm>

using namespace std;
const int INF = 1001001001;
#define rep(i,n) for(int i=0;i<n;i++)
int M,N,K;
char map[1002][1002];
int rmap[1002][1002][3];


int main(){
	scanf("%d %d",&M,&N);
	scanf("%d",&K);
	rep(i,3){rmap[0][0][i]=0;}
	/////
	rep(i,M){
		rep(j,N){
			char a;
			scanf("%c",&a);
			if(a==10){scanf("%c",&a);}
			int s=0;
			map[i][j]=a;
			if(map[i][j]=='J')s=0;
			else if(map[i][j]=='O')s=1;
			else s=2;
			rep(k,3){
				int p=(int)(s==k);
				if(i==0&&j==0){
					rmap[i][j][k]+=p;
				}else if(i==0){
					rmap[i][j][k]+=rmap[i][j-1][k]+p;
				}else if(j==0){
					rmap[i][j][k]+=rmap[i-1][j][k]+p;
				}else{
					rmap[i][j][k]+=rmap[i][j-1][k]+rmap[i-1][j][k]-rmap[i-1][j-1][k]+p;
				}
			}
		}
	}
	rep(i,K){
		int a,b,c,d;
		scanf("%d %d %d %d",&a,&b,&c,&d);
		a--;b--;c--;d--;
		rep(j,3){
			int ans=rmap[c][d][j];
			if(a>0)ans-=rmap[a-1][d][j];
			if(b>0)ans-=rmap[c][b-1][j];
			if(a>0&&b>0)ans+=rmap[a-1][b-1][j];
			printf("%d",ans);
			if(j!=2)printf(" ");
		}
		printf("\n");
	}


}