#include <bits/stdc++.h>
using namespace std;
long long waku[1003][1003][3];
void fir() {
  for (long long i = 0; i < 1003; i++) {
    for (long long j = 0; j < 1003; j++) {
      for (long long k = 0; k < 3; k++) {
        waku[i][j][k] = 0;
      }
    }
  }
}
signed main() {
  char s;
  long long w, h, n;
  fir();
  cin >> h >> w >> n;
  for (long long i = 1; i < h + 1; i++) {
    for (long long j = 1; j < w + 1; j++) {
      cin >> s;
      for (long long k = 0; k < 3; k++)
        waku[i][j][k] +=
            (waku[i - 1][j][k] + waku[i][j - 1][k] - waku[i - 1][j - 1][k]);
      if (s == 'J') {
        waku[i][j][0]++;
      } else if (s == 'O') {
        waku[i][j][1]++;
      } else {
        waku[i][j][2]++;
      }
    }
  }
  cout << endl;
  long long lw, lh, rw, rh;
  for (long long i = 0; i < n; i++) {
    cin >> lw >> lh >> rw >> rh;
    for (long long j = 0; j < 3; j++) {
      long long ans = waku[rw][rh][j] + waku[lw - 1][lh - 1][j] -
                      waku[rw][lh - 1][j] - waku[lw - 1][rh][j];
      cout << ans;
      if (j < 2)
        cout << " ";
      else
        cout << endl;
    }
  }
  return 0;
}
