#include <cstdio>
#include <iostream>

using namespace std;

class JOI
{
	public:
	int J,O,I;
	
	JOI()
	{
		J = 0;
		O = 0;
		I = 0;
	}
	JOI operator+ (JOI &b){
		JOI c;
		c.J = J + b.J;
		c.O = O + b.O;
		c.I = I + b.I;
		return c;
	}
	JOI operator- (JOI &b){
		JOI c;
		c.J = J - b.J;
		c.O = O - b.O;
		c.I = I - b.I;
		return c;
	}
	void print(){
		printf("%d %d %d\n",J,O,I);
	}
};

JOI world[1002][1002];

int main()
{
	JOI ans;
	int m,n,k,x1,x2,y1,y2;
	char feeld[1000][1001];

	cin >> m >> n >> k;

	for(int i =0 ;i < m; i++){
		cin >> feeld[i];
	}

	for(int i = 1; i <= m; i++){
		for(int j = 1; j <= n; j++){
			world[i][j] = world[i][j-1] + world[i-1][j] -world[i-1][j-1];
			switch(feeld[i-1][j-1]){
				case 'J':
					world[i][j].J++;
					break;
				case 'O':
					world[i][j].O++;
					break;
				default:
					world[i][j].I++;
			}
		}
	}
	
	for(int i = 0; i < k; i++){
		cin >> y1 >> x1 >> y2 >> x2;
		ans = world[y2][x2] - world[y2][x1-1] - world[y1-1][x2] + world[y1-1][x1-1];
		ans.print();
	}

	return 0;
}