M,N = map(int,raw_input().split())
K = int(raw_input())
A = [raw_input() for i in range(M)]
for roop in range(K):
    a,b,c,d = map(int,raw_input().split())
    j = o = i = 0
    for m in range(a-1,c):
        j += A[m][b-1:d].count("J")
        o += A[m][b-1:d].count("O")
        i += A[m][b-1:d].count("I")
    print j,o,i