#include <bits/stdc++.h>
using namespace std;
static const long long INF = 1LL << 60LL;
string Map[10100];
long long J[1010][1010], O[1010][1010], I[1010][1010];
long long m, n, k;
long long a, b, c, d;
long long res_J, res_O, res_I;
int main() {
  cin >> m >> n >> k;
  for (int y = 1; y <= m; ++y) {
    for (int x = 1; x <= n; ++x) {
      cin >> Map[y][x];
      if (Map[y][x] == 'J') {
        J[y][x]++;
      }
      if (Map[y][x] == 'O') {
        O[y][x]++;
      }
      if (Map[y][x] == 'I') {
        I[y][x]++;
      }
      J[y][x] += J[y][x - 1];
      O[y][x] += O[y][x - 1];
      I[y][x] += I[y][x - 1];
    }
  }
  for (int i = 1; i <= k; ++i) {
    cin >> a >> b >> c >> d;
    for (int y = a; y <= c; ++y) {
      res_J += J[y][d] - J[y][b - 1];
      res_O += O[y][d] - O[y][b - 1];
      res_I += I[y][d] - I[y][b - 1];
    }
    cout << res_J << " " << res_O << " " << res_I << endl;
    res_J = 0;
    res_O = 0;
    res_I = 0;
  }
  return 0;
}
