#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int jcount;
  int ocount;
  int icount;
} JOI;
int main() {
  JOI joi[1000][1000];
  char masu[1000][1000];
  int m, n, k;
  int J = 0, O = 0, I = 0;
  int x, y, xx, yy;
  cin >> m >> n >> k;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> masu[i][j];
      if (masu[i][j] == 'J') {
        J = J + 1;
      } else if (masu[i][j] == 'O') {
        O = O + 1;
      } else {
        I = I + 1;
      }
      joi[i][j].jcount = J;
      joi[i][j].ocount = O;
      joi[i][j].icount = I;
    }
    J = 0;
    O = 0;
    I = 0;
  }
  for (int i = 0; i < k; i++) {
    cin >> x >> y >> xx >> yy;
    int countj = 0, counto = 0, counti = 0;
    for (int j = x - 1; j < xx; j++) {
      countj = countj + joi[j][yy - 1].jcount - joi[j][y - 1].jcount;
      counto = counto + joi[j][yy - 1].ocount - joi[j][y - 1].ocount;
      counti = counti + joi[j][yy - 1].icount - joi[j][y - 1].icount;
      if (masu[j][y - 1] == 'J') {
        countj = countj + 1;
      } else if (masu[j][y - 1] == 'O') {
        counto = counto + 1;
      } else {
        counti = counti + 1;
      }
    }
    cout << countj << " " << counto << " " << counti << endl;
  }
}
