#include <bits/stdc++.h>
using namespace std;
int main() {
  int M, N, K;
  int a, b, c, d;
  char map[1010][1010];
  cin >> M >> N >> K;
  for (int i = 0; i < M; i++) {
    cin >> map[i];
  }
  for (int i = 0; i < K; i++) {
    int cnt[3] = {0};
    cin >> a >> b >> c >> d;
    for (int j = a - 1; j < c; j++) {
      for (int k = b - 1; k < d; k++) {
        if (map[j][k] == 'J') {
          cnt[0]++;
        } else if (map[j][k] == 'O') {
          cnt[1]++;
        } else {
          cnt[2]++;
        }
      }
    }
    cout << cnt[0] << " " << cnt[1] << " " << cnt[2] << endl;
  }
  return 0;
}
