#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
char MM[1005][1003];
int MO[1005][1003]={},MJ[1005][1003]={},MI[1005][1003]={};
int main(){

	string str;
	int y,x,n;
	cin>>y>>x;
	cin>>n;
	for(int i=1;i<=y;i++)
	{cin>>str;
	for(int j=0;j<str.size();j++)
		{
			MM[i][j+1]=str[j];
			if(str[j]=='O')MO[i][j+1]++;
			if(str[j]=='J')MJ[i][j+1]++;
			if(str[j]=='I')MI[i][j+1]++;

			MO[i][j+1]+=MO[i][j];
			MJ[i][j+1]+=MJ[i][j];
			MI[i][j+1]+=MI[i][j];


	}
	}
	for(int I=0;I<n;I++){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		int J=0,U=0,II=0;
		for(int i=a;i<=c;i++){
			U+=MO[i][d]-MO[i][b-1];
			J+=MJ[i][d]-MJ[i][b-1];
			II+=MI[i][d]-MI[i][b-1];
			/*
			for(int j=b;j<=d;j++){
				if(MM[i][j]=='O')U++;
				if(MM[i][j]=='J')J++;
				if(MM[i][j]=='I')II++;

			//	cout<<MM[i][j];
			}*/
		//	cout<<endl;
		}

			cout<<J<<" "<<U<<" "<<II<<endl;
	}
	return 0;
}