#include <cstdio>
int H,W,K;
int dp[3][1001][1001]={};
char in[1001];
int main(){
	scanf("%d%d%d",&H,&W,&K);
	for(int i = 1 ; i <= H ; i++){
		scanf("%s",in);
		for(int j = 0 ; j < W ; j++){
			dp[0][i][j+1] = dp[0][i][j];
			dp[1][i][j+1] = dp[1][i][j];
			dp[2][i][j+1] = dp[2][i][j];
			if(in[j] == 'J')		dp[0][i][j+1] ++;
			else if(in[j] == 'O') 	dp[1][i][j+1] ++;
			else					dp[2][i][j+1] ++;
		}
	}
	for(int i = 1; i <= H ; i++)
		for(int j = 1 ; j <= W ; j++)
			for(int k = 0 ; k < 3 ; k++)
				dp[k][i][j] += dp[k][i-1][j];
	for(int i = 0 ; i < K ; i++){
		int a,b,c,d,r[3]; scanf("%d%d%d%d",&a,&b,&c,&d);
		for(int j = 0 ; j < 3 ; j++) r[j] = dp[j][c][d] - dp[j][c][b-1] - dp[j][a-1][d] + dp[j][a-1][b-1];
		printf("%d %d %d\n",r[0],r[1],r[2]);
	}
	
}