M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
J,O,I = [[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)],[[0]*(N+1) for i in range(M)]
for m in range(M):
    j = o = i = 0
    for n in range(N):
        if   A[m][n] == "J": j += 1
        elif A[m][n] == "O": o += 1
        elif A[m][n] == "I": i += 1
        J[m][n+1],O[m][n+1],I[m][n+1] = j,o,i

for roop in range(K):
    a,b,c,d = map(int,raw_input().split())
    try:
        j = sum(J[m][d] - J[m][b-1] for m in range(a-1,c))
        o = sum(O[m][d] - O[m][b-1] for m in range(a-1,c))
        i = sum(I[m][d] - I[m][b-1] for m in range(a-1,c))
    except:
        pass
    print j,o,i