#include <cstdio>

using namespace std;

const int MAX_M=1000,MAX_N=1000;
const int MAX_K=100000;

int m,n;
int k;
char field[MAX_N+1];
struct type{
	int j,o,i;
};
type operator +(const type &a,const type &b){
	type c;
	c.j=a.j+b.j;
	c.o=a.o+b.o;
	c.i=a.i+b.i;
	return c;
}
type operator -(const type &a,const type &b){
	type c;
	c.j=a.j-b.j;
	c.o=a.o-b.o;
	c.i=a.i-b.i;
	return c;
}
type table[MAX_M+1][MAX_N+1];
type res[MAX_K];

int main(){
	scanf("%d %d",&m,&n);
	scanf("%d",&k);
	for (int y=0;y<m;y++){
		scanf("%s",field);
		for (int x=0;x<n;x++){
			table[y+1][x+1]=table[y][x+1]+table[y+1][x]-table[y][x];
			switch (field[x]){
				case 'J':
					table[y+1][x+1].j++;
					break;
				case 'O':
					table[y+1][x+1].o++;
					break;
				case 'I':
					table[y+1][x+1].i++;
					break;
			}
		}
	}
	for (int p=0;p<k;p++){
		int a,b,c,d;
		scanf("%d %d %d %d",&a,&b,&c,&d);
		res[p]=table[c][d]-table[c][b-1]-table[a-1][d]+table[a-1][b-1];
	}
	for (int p=0;p<k;p++){
		printf("%d %d %d\n",res[p].j,res[p].o,res[p].i);
	}
	return 0;
}