#include<bits/stdc++.h>
#define LL long long
using namespace std;
static const LL INF = 1LL<<60LL;
typedef pair<int,int> pii;

char Map[1010][1010];

LL J[1010][1010],O[1010][1010],I[1010][1010];
LL m,n,k;
LL a,b,c,d;
LL res_J,res_O,res_I;

int main(){
    cin>>m>>n;
    cin>>k;
    for(int y=1;y<=m;++y){
        for(int x=1;x<=n;++x){
            cin>>Map[y][x];
            if(Map[y][x]=='J'){
                J[y][x]++;
            }
            if(Map[y][x]=='O'){
                O[y][x]++;
            }
            if(Map[y][x]=='I'){
                I[y][x]++;
            }
            J[y][x]+=J[y][x-1];
            O[y][x]+=O[y][x-1];
            I[y][x]+=I[y][x-1];
        }
    }
    for(int i=0;i<k;++i){
        cin>>a>>b>>c>>d;
        int Min=min(a,c);
        int Max=max(a,c);
        for(int y=Min;y<=Max;++y){
            res_J+=J[y][d]-J[y][b-1];
            res_O+=O[y][d]-O[y][b-1];
            res_I+=I[y][d]-I[y][b-1];
        }
        cout<<res_J<<" "<<res_O<<" "<<res_I<<endl;
        res_J=0;
        res_O=0;
        res_I=0;
    }
    return 0;
}