#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9, N = 1010;
const long long INFL = 1e18;
int n, m, k;
char a[N][N];
int T[N][N];
struct query {
  int X1;
  int Y1;
  int X2;
  int Y2;
  int J;
  int O;
  int I;
} q[N * 10];
void upd(int x, int y) {
  for (int i = x; i <= n; i += i & -i)
    for (int j = y; j <= m; j += j & -j) T[i][j]++;
}
int get(int x, int y) {
  int res = 0;
  for (int i = x; i > 0; i -= i & -i)
    for (int j = y; j > 0; j -= j & -j) res += T[i][j];
  return res;
}
int get(query qu) {
  return get(qu.X2, qu.Y2) - get(qu.X1 - 1, qu.Y2) - get(qu.X2, qu.Y1 - 1) +
         get(qu.X1 - 1, qu.Y1 - 1);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m >> k;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) cin >> a[i][j];
  for (int i = 1; i <= k; i++) cin >> q[i].X1 >> q[i].Y1 >> q[i].X2 >> q[i].Y2;
  memset(T, 0, sizeof T);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'J') upd(i, j);
  for (int i = 1; i <= k; i++) q[i].J = get(q[i]);
  memset(T, 0, sizeof T);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'O') upd(i, j);
  for (int i = 1; i <= k; i++) q[i].O = get(q[i]);
  memset(T, 0, sizeof T);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (a[i][j] == 'I') upd(i, j);
  for (int i = 1; i <= k; i++) q[i].I = get(q[i]);
  for (int i = 1; i <= k; i++) printf("%d %d %d\n", q[i].J, q[i].O, q[i].I);
}
