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