#include <iostream>

#define calc(x) (x[u][v]-x[u][t-1]-x[s-1][v]+x[s-1][t-1])

using namespace std;

int jn[1001][1001];
int oc[1001][1001];
int ic[1001][1001];

int main()
{
  char c;
  int n, m, k, s, t, u, v;

  while(cin>>m>>n && n+m){
    cin >> k;
    for(int i = 0; i <= m; ++i)
      for(int j = 0; j <= n; ++j)
	jn[i][j] = oc[i][j] = ic[i][j] = 0;

    for(int i = 1; i <= m; ++i){
      s = t = u = 0;
      for(int j = 1; j <= n; ++j){
	cin >> c;
	if(c == 'J') ++s;
	else if(c == 'O') ++t;
	else ++u;

	jn[i][j] += (s+jn[i-1][j]);
	oc[i][j] += (t+oc[i-1][j]);
	ic[i][j] += (u+ic[i-1][j]);
      }
    }

    while(k--){
      cin >> s >> t >> u >> v;
      cout << calc(jn) << " " << calc(oc) << " " << calc(ic) << endl;
    }
  }

  return 0;
}