#include <bits/stdc++.h>
using namespace std;
int e[1000][1000];
int f[1000][20][3];
int main() {
  int h, w, k;
  cin >> h >> w;
  cin >> k;
  int sum = 0;
  for (int i = 0; i < h; i++) {
    string s;
    cin >> s;
    for (int j = 0; j < w; j++) {
      if (s[j] == 'J')
        e[i][j] = 0;
      else if (s[j] == 'O')
        e[i][j] = 1;
      else
        e[i][j] = 2;
    }
    sum = 0;
    for (int j = 0; j < floor(w / 50); j++) {
      f[i][j][0] = 0;
      f[i][j][1] = 0;
      f[i][j][2] = 0;
      for (int z = j * 50; z < (j + 1) * 50; z++) {
        f[i][j][e[i][j]]++;
      }
    }
  }
  for (int z = 0; z < k; z++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;
    int sum[3] = {0, 0, 0};
    for (int i = a; i <= c; i++) {
      for (int j = b; j <= d; j++) {
        if (j % 50 == 0 && j + 49 <= d) {
          sum[0] += f[i][j / 50][0];
          sum[1] += f[i][j / 50][1];
          sum[2] += f[i][j / 50][2];
          j += 49;
        } else {
          sum[e[i][j]]++;
        }
      }
    }
    cout << sum[0] << " " << sum[1] << " " << sum[2] << endl;
  }
}
