n,m=map(int,input().split())
j=[[0 for i in range(m+1)] for j in range(n+1)]
o=[[0 for i in range(m+1)] for j in range(n+1)]
i=[[0 for i in range(m+1)] for j in range(n+1)]
field=[]
roop=int(input())
for k in range(n):
    field.append(input())
for k in range(n):
    j_c,o_c,i_c=0,0,0
    for l in range(m):
        if field[k][l]=="J":
                j_c+=1
        if field[k][l]=="O":
                o_c+=1
        if field[k][l]=="I":
                i_c+=1
        j[k+1][l+1]=j[k][l+1]+j_c
        o[k+1][l+1]=o[k][l+1]+o_c
        i[k+1][l+1]=i[k][l+1]+i_c
for k in range(roop):
    x1,y1,x2,y2=map(int,input().split())
    j_c=j[x2][y2]-j[x2][y1-1]-j[x1-1][y2]+j[x1-1][y1-1]
    o_c=o[x2][y2]-o[x2][y1-1]-o[x1-1][y2]+o[x1-1][y1-1]
    i_c=i[x2][y2]-i[x2][y1-1]-i[x1-1][y2]+i[x1-1][y1-1]
    print(j_c,o_c,i_c)
