#include <iostream>
#include <cstdio>
#include <string>
using namespace std;

string maze[1001];
int jcnt[1010][1010];
int ocnt[1010][1010];
int icnt[1010][1010];

int main() {
	int m, n, k;
	cin >> m >> n;
	cin >> k;
	for(int i = 0; i < m; ++i){
		cin >> maze[i];
	}
	for(int i = 0; i < m; ++i){
		for(int j = 0; j < n; ++j){
			jcnt[i + 1][j + 1] = jcnt[i + 1][j] + jcnt[i][j + 1] 
								- jcnt[i][j] + (maze[i][j] == 'J' ? 1 : 0);
			ocnt[i + 1][j + 1] = ocnt[i + 1][j] + ocnt[i][j + 1] 
								- ocnt[i][j] + (maze[i][j] == 'O' ? 1 : 0);
			icnt[i + 1][j + 1] = icnt[i + 1][j] + icnt[i][j + 1] 
								- icnt[i][j] + (maze[i][j] == 'I' ? 1 : 0);
		}
	}
	int a, b, c, d;
	for(int i = 0; i < k; ++i){
		scanf("%d %d %d %d", &a, &b, &c, &d);

		printf("%d", jcnt[c][d] - jcnt[c][b - 1] - jcnt[a - 1][d] + jcnt[a - 1][b - 1]);
		printf(" %d", ocnt[c][d] - ocnt[c][b - 1] - ocnt[a - 1][d] + ocnt[a - 1][b - 1]);
		printf(" %d\n", icnt[c][d] - icnt[c][b - 1] - icnt[a - 1][d] + icnt[a - 1][b - 1]);
	}
	return 0;
}