#include <iostream>
using namespace std;

int  W, H, K;
int J[1001][1001] = {0};
int O[1001][1001] = {0};
int I[1001][1001] = {0};
string m[1001];

// [x1,x2] && [y1,y2] の領域の総和を計算する.
int sum_J(int x1, int y1, int x2, int y2){
	return J[y1][x1] - J[y1][x2+1] - J[y2+1][x1] + J[y2+1][x2+1];
}
int sum_O(int x1, int y1, int x2, int y2){
	return O[y1][x1] - O[y1][x2+1] - O[y2+1][x1] + O[y2+1][x2+1];
}
int sum_I(int x1, int y1, int x2, int y2){
	return I[y1][x1] - I[y1][x2+1] - I[y2+1][x1] + I[y2+1][x2+1];
}

int main(){
	cin >> H >> W >> K;
	for(int y = 0; y < H; y++){
		cin >> m[y];
	}
	
	for(int y = 0; y < H; y++){
		for(int x = 0; x < W; x++){
			J[y][x] = (m[y][x] == 'J')? 1 : 0;
			O[y][x] = (m[y][x] == 'O')? 1 : 0;
			I[y][x] = (m[y][x] == 'I')? 1 : 0;
		}
	}
	// 累積和の計算
	for(int y = 0 ; y < H ; y++ ){
		for(int x = W-1 ; x >= 0 ; x-- ){
			J[y][x] += J[y][x+1];
			O[y][x] += O[y][x+1];
			I[y][x] += I[y][x+1];
		}
	}
	for(int y = H-1 ; y >= 0 ; y-- ){
		for(int x = 0 ; x < W ; x++ ){
			J[y][x] += J[y+1][x];
			O[y][x] += O[y+1][x];
			I[y][x] += I[y+1][x];
		}
	}
	
	for(int i = 0; i < K; i++){
		int x1, y1, x2, y2;
		cin >> y1 >> x1 >> y2 >> x2;
		x1--; y1--; x2--; y2--;
		cout << sum_J(x1, y1, x2, y2) << " " << sum_O(x1, y1, x2, y2) << " " << sum_I(x1, y1, x2, y2) << endl;
	}
}