#include <bits/stdc++.h>
using namespace std;
char MM[1005][1003];
int main() {
  string str;
  int y, x, n;
  cin >> y >> x;
  cin >> n;
  for (int i = 1; i <= y; i++) {
    cin >> str;
    for (int j = 0; j < str.size(); j++) MM[i][j + 1] = str[j];
  }
  for (int I = 0; I < n; I++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int J = 0, U = 0, II = 0;
    for (int i = a; i <= c; i++) {
      for (int j = b; j <= d; j++) {
        if (MM[i][j] == 'O') U++;
        if (MM[i][j] == 'J') J++;
        if (MM[i][j] == 'I') II++;
      }
    }
    cout << J << " " << U << " " << II << endl;
  }
  return 0;
}
