from array import array

M, N = map(int, raw_input().split())
K = input()

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

for i in xrange(1, M+1):
    a = raw_input()
    for j in xrange(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 xrange(K):
    b0, b1, b2, b3 = map(int, raw_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