#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
const double EPS = 1e-10;
const double PI = acos(-1.0);
char c[] = {'J', 'O', 'I'};
int main() {
  int m, n;
  cin >> m >> n;
  int k;
  cin >> k;
  vector<string> field(m);
  for (int i = (0); i < (m); ++i) {
    cin >> field[i];
  }
  vector<vector<vector<int> > > joi(
      3, vector<vector<int> >(m + 1, vector<int>(n + 1)));
  for (int i = (0); i < (3); ++i) {
    for (int y = (1); y < (m + 1); ++y) {
      for (int x = (1); x < (n + 1); ++x) {
        joi[i][y][x] = joi[i][y][x - 1] + (field[y - 1][x - 1] == c[i]);
      }
    }
  }
  for (int i = (0); i < (3); ++i) {
    for (int y = (1); y < (m + 1); ++y) {
      for (int x = (1); x < (n + 1); ++x) {
        joi[i][y][x] += joi[i][y - 1][x];
      }
    }
  }
  for (int i = (0); i < (k); ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    for (int i = (0); i < (3); ++i) {
      cout << joi[i][c][d] - joi[i][c][b - 1] - joi[i][a - 1][d] +
                  joi[i][a - 1][b - 1]
           << (i == k - 1 ? "" : " ");
    }
    cout << endl;
  }
}
