#include <iostream>
#include <algorithm>
using namespace std;
int m, n, k;
char f[1005][1005];
int sum[3][1005][1005] = { 0 };
int main() {
	cin >> m >> n >> k;
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++) cin >> f[i][j];
	for (int u = 0; u < 3; u++) {
		char c;
		int cnt = 0;
		if (u == 0) c = 'J';
		if (u == 1) c = 'O';
		if (u == 2) c = 'I';
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				if (f[i][j] == c) sum[u][i][j] = 1;
			}
		}
		for (int i = 0; i < m; i++) {
			for (int j = 0; j < n; j++) {
				sum[u][i][j + 1] += sum[u][i][j];
			}
		}
		for (int j = 0; j < n; j++) {
			for (int i = 0; i < m; i++) {
				sum[u][i + 1][j] += sum[u][i][j];
			}
		}
	}
	for (int q = 0; q < k; q++) {
		int a, b, c, d;
		cin >> a >> b >> c >> d;
		a--; b--; c--; d--;
		for (int u = 0; u < 3; u++) {
			int res = 0;
			res += sum[u][c][d];
			if (a - 1 >= 0 && b - 1 >= 0) res += sum[u][a - 1][b - 1];
			if (a - 1 >= 0) res -= sum[u][a - 1][d];
			if (b - 1 >= 0) res -= sum[u][c][b - 1];
			if(u!=2) cout << res << " ";
			else cout << res << endl;
		}
	}
	return 0;
}
