#include <iostream>

using namespace std;

int H, W, K;
char map[1005][1005];
int sumJ[1005][1005], sumO[1005][1005], sumI[1005][1005];

int main(void)
{
	cin >> H >> W >> K;
	for(int y = 1; y <= H; y++){
		for(int x = 1; x <= W; x++){
			cin >> map[x][y];
		}
	}
	
	for(int y = 1; y <= H; y++){
		for(int x = 1; x <= W; x++){
			sumJ[x][y] = sumJ[x-1][y] - sumJ[x-1][y-1] + sumJ[x][y-1];
			sumO[x][y] = sumO[x-1][y] - sumO[x-1][y-1] + sumO[x][y-1];
			sumI[x][y] = sumI[x-1][y] - sumI[x-1][y-1] + sumI[x][y-1];
			if(map[x][y] == 'J') sumJ[x][y]++;
			if(map[x][y] == 'O') sumO[x][y]++;
			if(map[x][y] == 'I') sumI[x][y]++;
		}
	}
	
	int x1, y1, x2, y2;
	for(int k = 0; k < K; k++){
		cin >> y1 >> x1 >> y2 >> x2;
		cout << sumJ[x2][y2] + sumJ[x1-1][y1-1] - sumJ[x2][y1-1] - sumJ[x1-1][y2] << " ";
		cout << sumO[x2][y2] + sumO[x1-1][y1-1] - sumO[x2][y1-1] - sumO[x1-1][y2] << " ";
		cout << sumI[x2][y2] + sumI[x1-1][y1-1] - sumI[x2][y1-1] - sumI[x1-1][y2] << endl;
	}
	
	return 0;
}
