#include <bits/stdc++.h>
#define foru(i, a, b) for(int i = int(a); i <= int(b); i++)
using namespace std;

const int N = 1005;
const int K = 100005;

int n, m, k, ans1[K], ans2[K], ans3[K];
int x[K], y[K], a[K], b[K], t[N][N];
char s[N][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[]) {
    foru(i, 1, n) foru(j, 1, m) t[i][j] = 0;
    foru(i, 1, n) foru(j, 1, m)
        t[i][j] = t[i-1][j] + t[i][j-1] - t[i-1][j-1]
                + (s[i-1][j-1] == cc);
    foru(i, 1, k)
        ans[i] = rec(x[i], y[i], a[i], b[i]);
}

int main() {
    //freopen("a.txt", "r", stdin);

    scanf("%d%d%d", &n, &m, &k);
    foru(i, 0, n-1) scanf("%s", &s[i]);
    foru(i, 1, k) scanf("%d%d%d%d", &x[i], &y[i], &a[i], &b[i]);

    sol('J', ans1);  sol('O', ans2);  sol('I', ans3);

    foru(i, 1, k) printf("%d %d %d\n", ans1[i], ans2[i], ans3[i]);
}