#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <utility>
#include <stack>
#include <queue>
#include <sstream>
#include <fstream>
#include <cmath>
#include <cstring>
 
using namespace std;
#define int long long
const int mod = 1000000007;
 
int a[1000001] = {}, b[1000001] = {}, c[1000001] = {}, d[1000001] = {};
int memo[10001][10001][3]= {};
char w[10001][10001] = {};
 
signed main(){
    int M, N, K; cin >> M >> N >>K;
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            cin >> w[i][j];
        }
    }
    for (int i = 0; i < K; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
    for (int i = 0; i < M; i++) {
        int t1 = 0,t2=0,t3=0;
        for (int j = 0; j < N; j++) {
            if (w[i][j] == 'J')t1++;
            else if (w[i][j] == 'O')t2++;
            else t3++;
 
            if (i == 0) {
                memo[i][j][0] = t1;
                memo[i][j][1] = t2;
                memo[i][j][2] = t3;
            }
            else {
                memo[i][j][0] = memo[i-1][j][0] + t1;
                memo[i][j][1] = memo[i-1][j][1] + t2;
                memo[i][j][2] = memo[i-1][j][2] + t3;
            }
            //cout << " i" << i << " j" << j << " " << memo[i][j][0] << memo[i][j][1] << memo[i][j][2];
        }
        //cout << endl;
    }
    for (int i = 0; i < K; i++) {
        int a1, a2, a3;
        a1 = memo[c[i] - 1][d[i] - 1][0] - memo[a[i]-2][d[i]-1][0] - memo[c[i]-1][b[i]-2][0] + memo[a[i]-2][b[i]-2][0];
        a2 = memo[c[i] - 1][d[i] - 1][1] - memo[a[i] - 2][d[i] - 1][1] - memo[c[i] - 1][b[i] - 2][1] + memo[a[i] - 2][b[i] - 2][1];
        a3 = memo[c[i] - 1][d[i] - 1][2] - memo[a[i] - 2][d[i] - 1][2] - memo[c[i] - 1][b[i] - 2][2] + memo[a[i] - 2][b[i] - 2][2];
        cout << a1 << " " << a2 << " " << a3 << endl;
 
 
    }
     
}