#include <bits/stdc++.h>
using namespace std;
int M, N, K;
int a, b, c, d;
;
int joi[3], now;
char joic[3] = {'J', 'O', 'I'};
char island[1000 + 1][1000 + 1];
char island_bk[1000 + 1][1000 + 1];
int dm[4] = {-1, 0, 1, 0};
int dn[4] = {0, 1, 0, -1};
void solve(int m, int n) {
  joi[now]++;
  island[m][n] = '.';
  for (int i = 0; i < 4; i++) {
    int nm = m + dm[i], nn = n + dn[i];
    if (a <= nm && nm <= c && b <= nn && nn <= d &&
        island[nm][nn] == joic[now]) {
      solve(nm, nn);
    }
  }
}
void copy() {
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      island[i][j] = island_bk[i][j];
    }
  }
}
int main(void) {
  char e;
  cin >> M >> N;
  cin >> K;
  for (int i = 1; i <= M; i++) {
    for (int j = 1; j <= N; j++) {
      cin >> e;
      island[i][j] = e;
      island_bk[i][j] = e;
    }
  }
  for (int i = 0; i < K; i++) {
    joi[0] = 0, joi[1] = 0, joi[2] = 0;
    cin >> a >> b >> c >> d;
    for (int j = a; j <= c; j++) {
      for (int k = b; k <= d; k++) {
        if (island[j][k] == 'J') {
          now = 0;
          solve(j, k);
        } else if (island[j][k] == 'O') {
          now = 1;
          solve(j, k);
        } else if (island[j][k] == 'I') {
          now = 2;
          solve(j, k);
        }
      }
    }
    cout << joi[0] << " " << joi[1] << " " << joi[2] << endl;
    copy();
  }
  return 0;
}
