#include<bits/stdc++.h>

#define int long long
#define rep(a,b,c) for(int a=b;a<c;a++)

using namespace std;

int waku[1003][1003][3];

void fir(){
    for(int i=0;i<1003;i++){
        for(int j=0;j<1003;j++){
            for(int k=0;k<3;k++){
                waku[i][j][k]=0;
            }
        }
    }
}

signed main()
{
    char s;
    int w,h,n;
    fir();
    cin >> h >> w >> n;
    rep(i,1,h+1){
        rep(j,1,w+1){
            cin >> s;
            for(int k=0;k<3;k++)waku[i][j][k]+=(waku[i-1][j][k]+waku[i][j-1][k]-waku[i-1][j-1][k]);
            if(s=='J'){
                waku[i][j][0]++;
            }else if(s=='O'){
                waku[i][j][1]++;
            }else{
                waku[i][j][2]++;
            }
        }
    }
    /**
        debag
    rep(i,1,h+1){
        rep(j,1,w+1){
            cout << waku[i][j][0] << " ";
        }
        cout << endl;
    }
    */
    int ans[3];
    int lw,lh,rw,rh;
    rep(i,0,n){
        cin >> lw >> lh >> rw >> rh;
        rep(j,0,3){
            ans[j] = waku[rw][rh][j]+waku[lw-1][lh-1][j]-waku[rw][lh-1][j]-waku[lw-1][rh][j];
        }
        cout << ans[0] << " " << ans[1] << " " << ans[2] << endl;
    }
    return 0;
}