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]

D = {}

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

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