#include <bits/stdc++.h>
using namespace std;
const long long INF = 114514810;
const long long MOD = 1000000007;
const double EPS = 1e-8;
const double PI = acos(-1.0);
struct edge {
  int from, to, cost;
  bool operator<(const edge& e) const { return cost < e.cost; }
  bool operator>(const edge& e) const { return cost > e.cost; }
};
int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};
bool valid(int x, int y, int h, int w) {
  return (x >= 0 && y >= 0 && x < h && y < w);
}
int main() {
  int m, n, k;
  cin >> m >> n >> k;
  vector<string> fld(m);
  for (int i = (0); i < (m); i++) cin >> fld[i];
  int sj[55][55] = {}, so[55][55] = {}, si[55][55] = {};
  for (int i = (1); i < (m + 1); i++)
    for (int j = (1); j < (n + 1); j++) {
      sj[i][j] = sj[i - 1][j] + sj[i][j - 1] - sj[i - 1][j - 1];
      if (fld[i - 1][j - 1] == 'J') sj[i][j]++;
      so[i][j] = so[i - 1][j] + so[i][j - 1] - so[i - 1][j - 1];
      if (fld[i - 1][j - 1] == 'O') so[i][j]++;
      si[i][j] = si[i - 1][j] + si[i][j - 1] - si[i - 1][j - 1];
      if (fld[i - 1][j - 1] == 'I') si[i][j]++;
    }
  for (int i = (0); i < (k); i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    cout << sj[c][d] - sj[a - 1][d] - sj[c][b - 1] + sj[a - 1][b - 1] << " "
         << so[c][d] - so[a - 1][d] - so[c][b - 1] + so[a - 1][b - 1] << " "
         << si[c][d] - si[a - 1][d] - si[c][b - 1] + si[a - 1][b - 1] << endl;
  }
  return 0;
}
