M, N = map(int, raw_input().split())
K = input()

d = {}

for i in range(M):
    a = raw_input()
    for j in range(N):
        d[i+1, j+1] = a[j]

for _ in range(K):
    b = map(int, raw_input().split())
    D = {}
    D['J'] = D['O'] = D['I'] = 0
    for i in range(b[0], b[2]+1):
        for j in range(b[1], b[3]+1):
            D[d[i, j]] += 1
    print D['J'], D['O'], D['I']