line =input()
w=line.split(" ")
height=int(w[0])
wide=int(w[1])

pattern_num=int(input())
a=[]
for i in range(pattern_num):
    a.append(input())

for i in range(pattern_num):
    y=input()
    north=int(y[0])-1
    west=int(y[2])-1
    south=int(y[4])-1
    east=int(y[6])-1
    jungle=0
    ocean=0
    ice=0

    for j in range(north,south+1):
        for k in range(west,east+1):
            if a[j][k] == "J":
                jungle+=1
            elif a[j][k] == "O":
                ocean+=1
            elif a[j][k] == "I":
                ice+=1