# AOJ 0560: Planetary Exploration
# Python3 2018.6.30 bal4u

tr = { 'J':0, 'O':1, 'I':2 }

s = [[[0 for c in range(1002)] for r in range(1002)] for k in range(3)]
m, n = map(int, input().split())
k = int(input())
for r in range(1, m+1):
	buf = input().strip()
	for c in range(1, n+1):
		for i in range(3): s[i][r][c] = s[i][r-1][c] + (i == tr[buf[c-1]])
for i in range(k):
	r1, c1, r2, c2 = map(int, input().split())
	ans = [0]*3
	for j in range(3):
		for c in range(c1, c2+1): ans[j] += s[j][r2][c] - s[j][r1-1][c];
	print(*ans)
