#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdio.h>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <math.h>
#include <utility>
#include <string>
#include <ctype.h>
#include <cstring>
#include <cstdio>
#include <sstream>
#include <functional>

using namespace std;

#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 1145141919
#define ll long long
//#define scanf scanf_s

char isl[1005][1005];
int s[3][1005][1005];//0-J 1-O 2-I

int main()
{
	int m, n, q;
	cin >> m >> n >> q;
	FOR(i, 1, m + 1)FOR(j, 1, n + 1) cin >> isl[i][j];
	REP(i, m + 1)REP(k, 3) s[k][i][0] = 0;
	REP(j, n + 1)REP(k, 3) s[k][0][j] = 0;
	FOR(i, 1, m + 1)FOR(j, 1, n + 1)
	{
		REP(k, 3) s[k][i][j] = s[k][i - 1][j] + s[k][i][j - 1] - s[k][i - 1][j - 1];
		if (isl[i][j] == 'J') s[0][i][j]++;
		else if (isl[i][j] == 'O') s[1][i][j]++;
		else s[2][i][j]++;
	}
	
	int a, b, c, d, J, O, I;
	REP(l, q)
	{
		cin >> a >> b >> c >> d;
		J = s[0][c][d] - s[0][c][b - 1] - s[0][a - 1][d] + s[0][a - 1][b - 1];
		O = s[1][c][d] - s[1][c][b - 1] - s[1][a - 1][d] + s[1][a - 1][b - 1];
		I = s[2][c][d] - s[2][c][b - 1] - s[2][a - 1][d] + s[2][a - 1][b - 1];
		cout << J << " " << O << " " << I << endl;
	}
	return 0;
}