#include <bits/stdc++.h>
using namespace std;
int f[1000][1000];
int main(void) {
  int H, W, K;
  cin >> H >> W;
  cin >> K;
  for (int i = 0; i < H; i++) {
    string tmp;
    cin >> tmp;
    for (int j = 0; j < W; j++) {
      f[i][j] = tmp[j];
    }
  }
  for (int i = 0; i < K; i++) {
    int J, O, I;
    J = O = I = 0;
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    for (int p = a; p < c; p++) {
      for (int q = b; q < d; q++) {
        if (f[p][q] == 'J') J++;
        if (f[p][q] == 'O') O++;
        if (f[p][q] == 'I') I++;
      }
    }
    cout << J << " " << O << " " << I << endl;
  }
  return 0;
}
