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