#include <bits/stdc++.h>
using namespace std;
int M, N, K;
string field[1000];
char trg[4] = "JOI";
int cnt[3][1000][1001];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> M >> N >> K;
  for (int x = 0; x < M; x++) {
    cin >> field[x];
  }
  for (int x = 0; x < M; x++) {
    fill(cnt[0][x], cnt[0][x] + N + 1, 0);
    fill(cnt[1][x], cnt[1][x] + N + 1, 0);
    fill(cnt[2][x], cnt[2][x] + N + 1, 0);
  }
  for (int x = 0; x < M; x++) {
    for (int y = 1; y <= N; y++) {
      for (int i = 0; i < 3; i++) {
        cnt[i][x][y] = cnt[i][x][y - 1];
        if (field[x][y - 1] == trg[i]) {
          ++cnt[i][x][y];
        }
      }
    }
  }
  for (; K--;) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    int res[3] = {0};
    for (int x = a - 1; x < c; x++) {
      for (int i = 0; i < 3; i++) {
        res[i] += cnt[i][x][d] - cnt[i][x][b - 1];
      }
    }
    cout << res[0] << ' ' << res[1] << ' ' << res[2] << endl;
  }
  return 0;
}
