#include "stdio.h"

int H,W;
int K;
char map[1001][1001] = {};
int width[3][1002][1002] = {};
int height[3][1002][1002] = {};
int box[4];
int ans[3];

int main() {
	scanf("%d %d", &H, &W);
	scanf("%d", &K);
	for (int i = 0; i < H; i++) {
		scanf("%s", map[i]);
	}
	for (int i = 1; i <= H; i++) {
		for (int j = 1; j <= W; j++) {
			for (int k = 0; k < 3; k++) {
				width[k][i][j] = width[k][i][j - 1];
			}
			if (map[i-1][j-1] == 'J')width[0][i][j]++;
			else if (map[i-1][j-1] == 'O')width[1][i][j]++;
			else width[2][i][j]++;
		}
	}
	for (int i = 0; i < K; i++) {
		ans[0] = ans[1] = ans[2] = 0;
		scanf("%d %d %d %d", &box[0], &box[1], &box[2], &box[3]);
		for (int j = box[0]; j <= box[2]; j++) {
			for (int k = 0; k < 3; k++) {
				ans[k] += width[k][j][box[3]] - width[k][j][box[1]-1];
			}
		}
		printf("%d %d %d\n", ans[0], ans[1], ans[2]);
	}
	return 0;
}