#include <bits/stdc++.h>
using namespace std;
int map[1000][1000];
int main() {
  int m, n;
  long k;
  cin >> m >> n >> k;
  for (int i = 1; i <= m; ++i) {
    for (int i2 = 1; i2 <= n; ++i2) {
      char str;
      cin >> str;
      int data;
      if (str == 'J') {
        data = 0;
      } else if (str == 'O') {
        data = 1;
      } else {
        data = 2;
      }
      map[i][i2] = data;
    }
  }
  for (long i = 0; i < k; ++i) {
    int sy, sx, gy, gx;
    int cnt_j = 0, cnt_o = 0, cnt_i = 0;
    cin >> sy >> sx >> gy >> gx;
    for (int j = sy; j <= gy; ++j) {
      for (int j2 = sx; j2 <= gx; ++j2) {
        if (map[j][j2] == 0) {
          ++cnt_j;
        } else if (map[j][j2] == 1) {
          ++cnt_o;
        } else
          ++cnt_i;
      }
    }
    cout << cnt_j << " " << cnt_o << " " << cnt_i << endl;
  }
  return 0;
}
