#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n;
  int k;
  cin >> m >> n;
  cin >> k;
  vector<vector<char> > hyou(m, vector<char>(n));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> hyou[i][j];
    }
  }
  for (int i = 0; i < k; i++) {
    int counter[3] = {0};
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    for (int j = a - 1; j < c; j++) {
      for (int t = b - 1; t < d; t++) {
        if (hyou[j][t] == 'J') {
          counter[0]++;
        } else if (hyou[j][t] == 'O') {
          counter[1]++;
        } else {
          counter[2]++;
        }
      }
    }
    cout << counter[0] << ' ' << counter[1] << ' ' << counter[2] << endl;
  }
  return 0;
}
