#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[100000] = {}, b[100000] = {}, c[100000] = {}, d[100000] = {};
int memo[1000][1000][1000][3]= {};
char w[1000][1000] = {};

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++) {
		for (int j = 0; j < N; j++){
			int t1 = 0, t2 = 0, t3 = 0;
			for (int k = j; k < N; k++) {
				if (w[i][k] == 'J') t1++;
				else if (w[i][k] == 'O')t2++;
				else t3++;
				memo[j][k][i][0] = t1;
				memo[j][k][i][1] = t2;
				memo[j][k][i][2] = t3;
//<<"i"<<i<<" j"<<j<<" k"<<k<<" "<< t1 << " " << t2 << " " << t3 << endl;
			}
		}
	}
	
	for (int i = 0; i < K; i++) {
		int t1 = 0, t2 = 0, t3 = 0;
		for (int j = a[i]-1; j < c[i]; j++) {
			t1 += memo[b[i]-1][d[i]-1][j][0];
			t2 += memo[b[i]-1][d[i]-1][j][1];
			t3 += memo[b[i]-1][d[i]-1][j][2];
		}
		cout << t1 << " " << t2 << " " << t3 << endl;
	}
	
}