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

int main()
{
	int M, N, K;
	cin >> M >> N >> K;
	vector<vector<vector<int>>> x(3, vector<vector<int>>(M + 1, vector<int>(N + 1)));
	for (int i = 1; i <= M; i++) {
		for (int j = 1; j <= N; j++) {
			char c;
			cin >> c;
			if (c == 'J') {
				x[0][i][j] += 1;
			}
			else if (c == 'O') {
				x[1][i][j] += 1;
			}
			else {
				x[2][i][j] += 1;
			}
		}
	}
	for (int i = 0; i < 3; i++) {
		for (int j = 0; j <= M; j++) {
			for (int k = 1; k <= N; k++) {
				x[i][j][k] += x[i][j][k - 1];
			}
		}
		for (int k = 0; k <= N; k++) {
			for (int j = 1; j <= M; j++) {
				x[i][j][k] += x[i][j - 1][k];
			}
		}
	}
	while (K--) {
		int a, b, c, d;
		cin >> a >> b >> c >> d; a--, b--;
		for (int i = 0; i < 3; i++) {
			cout << (x[i][c][d] - x[i][a][d] - x[i][c][b] + x[i][a][b]) << " \n"[i + 1 == 3];
		}
	}
	return 0;
}

