m,n = map(int,input().split())
k = int(input())
c = [[] for i in range(m)]
for i in range(m):
    c[i] = input()
x = ["J","O","I"]

da = [[[0]*n for _ in range(m)] for _ in range(3)]

for l in range(3):
    if c[0][0] == x[l]:
        da[l][0][0] = 1
    else:
        da[l][0][0] = 0
        
    for i in range(1,n):
        if c[0][i] == x[l]:
            da[l][0][i] = da[l][0][i-1] + 1
        else:
            da[l][0][i] = da[l][0][i-1]
            
    for i in range(1,m):
        co = 0
        for j in range(n):
            if c[i][j] == x[l]:
                co += 1
            da[l][i][j] = da[l][i-1][j] + co
            
for i in range(k):
    p,q,x,y = map(int,input().split())
    p -= 1
    q -= 1
    x -= 1
    y -= 1
    ans = [0] * 3
    for j in range(3):
        if p == 0 and q == 0:
            ans[j] = da[j][x][y]
        elif p == 0:
            ans[j] = da[j][x][y] - da[j][x][q-1]
        elif q == 0:
            ans[j] = da[j][x][y] - da[j][p-1][y]
        else:
            ans[j] = da[j][x][y] - da[j][p-1][y] - da[j][x][q-1] + da[j][p-1][q-1]
            
    print(*ans)
