M, N = list(map(int, input().split()))

K = eval(input())



D = {}



for i in range(M+1):

    D[i, 0] = 0, 0

for j in range(N+1):

    D[0, j] = 0, 0



for i in range(1, M+1):

    a = input()

    for j in range(1, N+1):

        if a[j-1] == '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 a[j-1] == '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 = list(map(int, 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)
from array import array



M, N = list(map(int, input().split()))

K = eval(input())



D0 = [array('l', [0]) * (N+1) for _ in range(M+1)]

D1 = [array('l', [0]) * (N+1) for _ in range(M+1)]



for i in range(1, M+1):

    a = input()

    for j in range(1, N+1):

        D0[i][j] = D0[i-1][j] + D0[i][j-1] - D0[i-1][j-1] + (a[j-1] == 'J')

        D1[i][j] = D1[i-1][j] + D1[i][j-1] - D1[i-1][j-1] + (a[j-1] == 'O')



for _ in range(K):

    b0, b1, b2, b3 = list(map(int, input().split()))

    nJ = D0[b2][b3]-D0[b0-1][b3]-D0[b2][b1-1]+D0[b0-1][b1-1]

    nO = D1[b2][b3]-D1[b0-1][b3]-D1[b2][b1-1]+D1[b0-1][b1-1]

    nI = (b2-b0+1)*(b3-b1+1)-nJ-nO

    print(nJ, nO, nI)
M, N = list(map(int, input().split()))
K = eval(input())
D = {}
for i in range(M + 1):
    D[i, 0] = 0, 0
for j in range(N + 1):
    D[0, j] = 0, 0
for i in range(1, M + 1):
    a = input()
    for j in range(1, N + 1):
        if a[j - 1] == "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 a[j - 1] == "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 = list(map(int, 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)

from array import array

M, N = list(map(int, input().split()))
K = eval(input())
D0 = [array("l", [0]) * (N + 1) for _ in range(M + 1)]
D1 = [array("l", [0]) * (N + 1) for _ in range(M + 1)]
for i in range(1, M + 1):
    a = input()
    for j in range(1, N + 1):
        D0[i][j] = D0[i - 1][j] + D0[i][j - 1] - D0[i - 1][j - 1] + (a[j - 1] == "J")
        D1[i][j] = D1[i - 1][j] + D1[i][j - 1] - D1[i - 1][j - 1] + (a[j - 1] == "O")
for _ in range(K):
    b0, b1, b2, b3 = list(map(int, input().split()))
    nJ = D0[b2][b3] - D0[b0 - 1][b3] - D0[b2][b1 - 1] + D0[b0 - 1][b1 - 1]
    nO = D1[b2][b3] - D1[b0 - 1][b3] - D1[b2][b1 - 1] + D1[b0 - 1][b1 - 1]
    nI = (b2 - b0 + 1) * (b3 - b1 + 1) - nJ - nO
    print(nJ, nO, nI)

+from array import array + -D = {} -for i in range(M + 1): -    D[i, 0] = 0, 0 -for j in range(N + 1): -    D[0, j] = 0, 0 +D0 = [array("l", [0]) * (N + 1) for _ in range(M + 1)] +D1 = [array("l", [0]) * (N + 1) for _ in range(M + 1)] -        if a[j - 1] == "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 a[j - 1] == "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], -            ) +        D0[i][j] = D0[i - 1][j] + D0[i][j - 1] - D0[i - 1][j - 1] + (a[j - 1] == "J") +        D1[i][j] = D1[i - 1][j] + D1[i][j - 1] - D1[i - 1][j - 1] + (a[j - 1] == "O") -    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] +    nJ = D0[b2][b3] - D0[b0 - 1][b3] - D0[b2][b1 - 1] + D0[b0 - 1][b1 - 1] +    nO = D1[b2][b3] - D1[b0 - 1][b3] - D1[b2][b1 - 1] + D1[b0 - 1][b1 - 1]