#include<stdio.h>
int JOI[1024][1024][3];
char m[1024][1024];
int main()
{
	int W,H,K,x,y,k,a,b,c,d;
	scanf("%d%d%d",&H,&W,&K);
	for(y=0;y<H;++y)scanf("%s",m[y]);
	for(y=0;y<H;++y)for(x=0;x<W;++x)
	{
		for(k=0;k<3;++k)
			JOI[y+1][x+1][k]=JOI[y+1][x][k]+JOI[y][x+1][k]-JOI[y][x][k];
		JOI[y+1][x+1][c=m[y][x],c=='J'?0:c=='O'?1:2]++;
	}
	while(K--)
	{
		scanf("%d%d%d%d",&a,&b,&c,&d);
#define P(k) JOI[c][d][k]+JOI[a-1][b-1][k]-JOI[a-1][d][k]-JOI[c][b-1][k]
		printf("%d %d %d\n",P(0),P(1),P(2));
	}
	return 0;
}