#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <map>
#include <utility>
#include <functional>

using namespace std;

//????¬?????´???????

char f[1010][1010];
int cumsum[1010][1010][3];

int main() {
	int m, n, l;
	char *table = "JOI";

	cin >> m >> n >> l;
	for (int i = 1; i <= m; ++i) {
		for (int j = 1; j <= n; ++j) {
			cin >> f[i][j];
		}
	}

	for (int k = 0; k < 3; ++k) {
		for (int i = 1; i <= m; ++i) {
			for (int j = 1; j <= n; ++j) {
				cumsum[i][j][k] = cumsum[i][j - 1][k] + (f[i][j] == table[k] ? 1 : 0);
			}
		}
	}
	for (int k = 0; k < 3; ++k) {
		for (int j = 1; j <= n; ++j) {
			for (int i = 1; i <= m; ++i) {
				cumsum[i][j][k] += cumsum[i - 1][j][k];
			}
		}
	}

	int a, b, c, d;
	while (l--) {
		cin >> a >> b >> c >> d;
		for (int i = 0; i < 3; ++i) {
			cout << cumsum[c][d][i] - cumsum[c][b - 1][i] - cumsum[a - 1][d][i] + cumsum[a - 1][b - 1][i];

			if (i < 2)
				cout << " ";
		}
		cout << endl;
	}

	return 0;
}