sn_km, ew_km = map(int, raw_input().split())
seek_area = int(raw_input())
zone = sn_km*[ew_km*[0]]

def cnt_zone(idx, first, last, str_for_count):
    return int(str(zone[idx])[first:last].count(str_for_count))

for i in range(sn_km):
    zone[i] = raw_input()
    
for i in range(seek_area):
    J = O = I = 0
    a, b, c, d = map((lambda x, y: x+y), map(int, raw_input().split()), 4*[-1])
    for y in range(c - a + 1):
        J += cnt_zone(a+y, b, d+1, 'J')
        O += cnt_zone(a+y, b, d+1, 'O')
        I += cnt_zone(a+y, b, d+1, 'I')
    print "%d %d %d" % (J, O, I)