import sys

H, W = map(int, input().split())
K = int(input())
cumsum = [[0]*(W+1) for _ in [0]*(H+1)]
d1, d2 = 10**12, 10**6

for y, _up in enumerate(cumsum[:-1], start=1):
    acc = 0
    for x, (up, c) in enumerate(zip(_up[1:], sys.stdin.readline()), start=1):
        if c == "J":
            acc += d1
        elif c == "O":
            acc += d2
        else:
            acc += 1
        cumsum[y][x] = acc + up

ans = []
append = ans.append
for y1, x1, y2, x2 in (map(int, l.split()) for l in sys.stdin):
    x1 -= 1
    y1 -= 1
    v = cumsum[y2][x2] - cumsum[y1][x2] - cumsum[y2][x1] + cumsum[y1][x1]
    append("%d %d %d"%(v//d1, v%d1//d2, v%d2))

print(*ans, sep="\n")
