#include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
typedef long long ll;
struct P {
	ll j, o, i;
};
P operator+(P a, P b) {
	return P{ a.j + b.j,a.o + b.o,a.i + b.i };
}
P operator-(P a, P b) {
	return P{ a.j - b.j,a.o - b.o,a.i - b.i };
}
char a[1000][1000];
P b[1000][1000];
int main() {
	int h, w, n;
	cin >> h >> w >> n;
	for (int i = 0; i < h; i++) {
		string u;
		cin >> u;
		for (int j = 0; j < w; j++) {
			a[i][j] = u[j];
			if (j == 0) {
				if (u[0] == 'J')
					b[i][j] = P{ 1,0,0 };
				else if (u[0] == 'O') 
					b[i][j] = P{ 0,1,0 };
				else b[i][j] = P{ 0,0,1 };
			}
			else {
				b[i][j] = b[i][j - 1];
				if (u[j] == 'J')
					b[i][j].j++;
				else if (u[j] == 'O')
					b[i][j].o++;
				else b[i][j].i++;
			}
		}
	}
	for (int z = 0; z < n; z++) {
		int a, e, c, d;
		cin >> a >> e >> c >> d;
		a--; e--; c--; d--;
		P sum = { 0,0,0 };
		for (int i = a; i <= c; i++) {
			if (e == 0) sum = sum + (b[i][d]);
			else sum = sum + (b[i][d] - b[i][e-1]);
		}
		cout << sum.j << " " << sum.o << " " << sum.i << endl;
	}
}