#include <bits/stdc++.h>
using namespace std;
int X[2][1000][1000];
int TOP[2][1000];
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  char z;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < h; j++) TOP[i][j] = 0;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> z;
      if (z == 'J') {
        X[0][i][TOP[0][i]] = j + 1;
        TOP[0][i]++;
      }
      if (z == 'O') {
        X[1][i][TOP[1][i]] = j + 1;
        TOP[1][i]++;
      }
    }
  }
  int x[2], y[2];
  for (int u = 0; u < k; u++) {
    for (int i = 0; i < 2; i++) cin >> y[i] >> x[i];
    int ANS[2] = {0};
    for (int d = 0; d < 2; d++) {
      for (int i = y[0]; i <= y[1]; i++) {
        int left, right, midl, midr;
        left = 0, right = TOP[d][i - 1];
        while (true) {
          if (left >= right - 1) break;
          midl = (left + right) / 2;
          if (X[d][i - 1][midl] < x[0])
            left = midl;
          else
            right = midl;
        }
        if (midl > 0)
          if (X[d][i - 1][midl - 1] >= x[0]) midl--;
        if (X[d][i - 1][midl] < x[0]) midl++;
        left = 0, right = TOP[d][i - 1];
        while (true) {
          if (left >= right - 1) break;
          midr = (left + right) / 2;
          if (X[d][i - 1][midr] <= x[1])
            left = midr;
          else
            right = midr;
        }
        if (x[1] >= X[d][i - 1][midr]) midr++;
        if (midr > 0)
          if (x[1] < X[d][i - 1][midr - 1]) midr--;
        ANS[d] += midr - midl;
      }
    }
    int len = (x[1] - x[0] + 1) * (y[1] - y[0] + 1);
    cout << ANS[0] << " " << ANS[1] << " " << len - ANS[0] - ANS[1] << endl;
  }
  return 0;
}
