#include <bits/stdc++.h>
using namespace std;

using ll = long long int;
using P = pair<int, int>;
using P3 = pair<int,P>;
using PP = pair<P, P>;
constexpr int INF = 1<<30;
constexpr ll MOD = ll(1e9)+7;
constexpr int di[] = {0,1,0,-1};
constexpr int dj[] = {1,0,-1,0};

int cnt[3][1001][1001];

bool solve(){
    int N, M, K;
    cin >> N >> M >> K;
    vector<string> s(N);
    for(int i=0;i<N;i++){
        cin >> s[i];
    }
    char ch[3] = {'J','O','I'};
    for(int k=0;k<3;k++){
        for(int i=0;i<=N;i++){
            for(int j=0;j<M;j++){
                cnt[k][i+1][j+1] += cnt[k][i+1][j] + (i<N && s[i][j]==ch[k] ? 1 : 0);
            }
        }
    }
    for(int k=0;k<3;k++){
        for(int i=0;i<N;i++){
            for(int j=0;j<=M;j++){
                cnt[k][i+1][j+1] += cnt[k][i][j+1];
            }
        }
    }
    for(int i=0;i<K;i++){
        int a, b, c, d;
        cin >> a >> b >> c >> d;
        a--; b--;
        for(int j=0;j<3;j++){
            cout << cnt[j][c][d] - cnt[j][c][b] - cnt[j][a][d] + cnt[j][a][b] << " \n"[j==2];
        }
    }
    return true;
}

int main(){
    solve();
    return 0;
}

