#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int K = 100005;
int n, m, k, ans1[K], ans2[K], ans3[K];
int x[N], y[N], a[N], b[N], t[N][N];
string s[N];
char c[N];
int rec(int x, int y, int a, int b) {
  return t[a][b] - t[a][y - 1] - t[x - 1][b] + t[x - 1][y - 1];
}
void sol(char cc, int ans[]) {
  for (int i = int(1); i <= int(n); i++)
    for (int j = int(1); j <= int(m); j++) t[i][j] = 0;
  for (int i = int(1); i <= int(n); i++)
    for (int j = int(1); j <= int(m); j++)
      t[i][j] = t[i - 1][j] + t[i][j - 1] - t[i - 1][j - 1] + (s[i][j] == cc);
  for (int i = int(1); i <= int(k); i++) ans[i] = rec(x[i], y[i], a[i], b[i]);
}
int main() {
  scanf("%d%d%d", &n, &m, &k);
  for (int i = int(1); i <= int(n); i++) {
    scanf("%s", &c);
    s[i] = c;
    s[i] = '%' + s[i];
  }
  for (int i = int(1); i <= int(k); i++)
    scanf("%d%d%d%d", &x[i], &y[i], &a[i], &b[i]);
  sol('J', ans1);
  sol('O', ans2);
  sol('I', ans3);
  for (int i = int(1); i <= int(k); i++)
    printf("%d %d %d\n", ans1[i], ans2[i], ans3[i]);
}
