#include <bits/stdc++.h>
#define ll long long
#define INF 999999999
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 1003;
const int MAX_M = 1003;

int a[MAX_N][MAX_N];
int dp[MAX_M][MAX_N][3];
int m,n,K;

int main()
{
	scanf("%d%d",&n,&m);
	scanf("%d",&K);
	rep(i,n){
		string s;
		cin >> s;
		rep(j,m){
			if(s[j] == 'J'){
				a[i][j] = 0;
			}else if(s[j] == 'O'){
				a[i][j] = 1;
			}else{
				a[i][j] = 2;
			}
		}
	}
	rep(i,n+1){
		rep(k,3){
			dp[i][0][k] = 0;
		}
	}
	rep(j,m+1){
		rep(k,3){
			dp[0][j][k] = 0;
		}
	}
	rep(i,n){
		rep(j,m){
			rep(k,3){
				dp[i+1][j+1][k] = dp[i][j+1][k] + dp[i+1][j][k] - dp[i][j][k];
				if(a[i][j] == k){
					dp[i+1][j+1][k]++;
				}
			}
		}
	}
	rep(i,K){
		int p,q,r,s;
		scanf("%d%d%d%d",&p,&q,&r,&s);
		int res[3];
		rep(j,3){
			res[j] = dp[r][s][j] - dp[p-1][s][j] - dp[r][q-1][j] + dp[p-1][q-1][j];
		}
		printf("%d %d %d\n",res[0],res[1],res[2]);
	}
	return 0;
}