#include<cstdio>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

#define rectsum(dp) (dp[b][r]-dp[t][r]-dp[b][l]+dp[t][l])

int main(){
	int h,w,q; scanf("%d%d%d",&h,&w,&q);
	static char B[1000][1001];
	rep(i,h) scanf("%s",B[i]);

	static int dp_J[1001][1001];
	static int dp_O[1001][1001];
	static int dp_I[1001][1001];
	rep(i,h) rep(j,w) {
		dp_J[i+1][j+1]=dp_J[i+1][j]+dp_J[i][j+1]-dp_J[i][j]+(B[i][j]=='J'?1:0);
		dp_O[i+1][j+1]=dp_O[i+1][j]+dp_O[i][j+1]-dp_O[i][j]+(B[i][j]=='O'?1:0);
		dp_I[i+1][j+1]=dp_I[i+1][j]+dp_I[i][j+1]-dp_I[i][j]+(B[i][j]=='I'?1:0);
	}

	while(q--){
		int t,l,b,r; scanf("%d%d%d%d",&t,&l,&b,&r); t--; l--;
		printf("%d %d %d\n",rectsum(dp_J),rectsum(dp_O),rectsum(dp_I));
	}

	return 0;
}