from itertools import accumulate
from operator import add
line, row = [int(x) for x in input().split()]
k = int(input())
L=[]
J=[[0]*(row+1) for _ in range(line+1)]
O=[[0]*(row+1) for _ in range(line+1)]
I=[[0]*(row+1) for _ in range(line+1)]

for _ in range(line):
    L.append(input())

for i in range(line):
    for j in range(row):
        if L[i][j] == "J":
            J[i+1][j+1] = 1
        elif L[i][j] == "O":
            O[i+1][j+1] = 1
        else:
            I[i+1][j+1] = 1

for i in range(len(J)):
    J[i] = list(accumulate(J[i]))
    O[i] = list(accumulate(O[i]))
    I[i] = list(accumulate(I[i]))

for i in range(1,len(J)):
    J[i] = list(map(add, J[i], J[i-1]))
    O[i] = list(map(add, O[i], O[i-1]))
    I[i] = list(map(add, I[i], I[i-1]))

def fcheck(M, a, b, c, d):
    return M[c][d] + M[a][b] - M[c][b] - M[a][d]

for _ in range(k):
    a, b, c, d = [int(x)-1 for x in input().split()]
    j = o = i = 0
    c +=1
    d +=1
    print( fcheck(J,a,b,c,d), fcheck(O,a,b,c,d), fcheck(I, a, b, c,d))

