#include<bits/stdc++.h>
using namespace std;

int m, n, k;
int field[3][1010][1010];

int main()
{
  scanf("%d%d%d", &m, &n, &k);
  
  for(int i = 1; i <= m; i++)
    for(int j = 1; j <= n; j++){
      char in;
      cin >> in;
      for(int j2 = 0; j2 < 3; j2++)
	field[j2][i][j] = field[j2][i - 1][j] + field[j2][i][j - 1] - field[j2][i - 1][j - 1];
      if(in == 'J')
	field[0][i][j]++;
      else if(in == 'O')
	field[1][i][j]++;
      else
	field[2][i][j]++;
    }
  
  for(int i = 0; i < k; i++){
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    printf("%d %d %d\n",
	   field[0][c][d] + field[0][a - 1][b - 1] - field[0][a - 1][d] - field[0][c][b - 1],
	   field[1][c][d] + field[1][a - 1][b - 1] - field[1][a - 1][d] - field[1][c][b - 1],
	   field[2][c][d] + field[2][a - 1][b - 1] - field[2][a - 1][d] - field[2][c][b - 1]);
  }
  return 0;
}