from pprint import pprint
m, n = map(int, raw_input().split())
k = int(raw_input())

psum = [[[0] * 3 for x in xrange(n + 1)] for y in xrange(m + 1)]

for y in xrange(m):
    line = raw_input()
    for x in xrange(n):
        for i in xrange(3):
            psum[y + 1][x + 1][i] = psum[y][x + 1][i] + psum[y + 1][x][i] - psum[y][x][i]
        psum[y + 1][x + 1][ { 'J':0, 'O':1, 'I':2 }[line[x]] ] += 1

for k_ in xrange(k):
    a, b, c, d = map(int, raw_input().split())
    j = psum[c][d][0] - psum[a - 1][d][0] - psum[c][b - 1][0] + psum[a - 1][b - 1][0]
    o = psum[c][d][1] - psum[a - 1][d][1] - psum[c][b - 1][1] + psum[a - 1][b - 1][1]
    i = psum[c][d][2] - psum[a - 1][d][2] - psum[c][b - 1][2] + psum[a - 1][b - 1][2]
    print j, o, i