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]

dJ = {}
dO = {}

for i in range(M+1):
    for j in range(N+1):
        if i == 0 or j == 0:
            dJ[i, j] = dO[i, j] = 0
        elif d[i, j] == 'J':
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1] + 1
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1]
        elif d[i, j] == 'O':
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1]
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1] + 1
        else:
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1]
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1]

for _ in range(K):
    b0, b1, b2, b3 = map(int, raw_input().split())
    nJ = dJ[b2, b3]-dJ[b0-1, b3]-dJ[b2, b1-1]+dJ[b0-1, b1-1]
    nO = dO[b2, b3]-dO[b0-1, b3]-dO[b2, b1-1]+dO[b0-1, b1-1]
    nI = (b2-b0+1)*(b3-b1+1)-nJ-nO
    print nJ, nO, nI