M, N = map(int, raw_input().split())
K = input()

d = {}

for i in range(M):
    a = raw_input()
    for j in range(N):
        d[i+1, j+1] = a[j]

dJ = {}
dO = {}
dI = {}

for i in range(M+1):
    for j in range(N+1):
        if i == 0 or j == 0:
            dJ[i, j] = dO[i, j] = dI[i, j] = 0
        elif d[i, j] == 'J':
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1] + 1
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1]
            dI[i, j] = dI[i-1, j] + dI[i, j-1] - dI[i-1, j-1]
        elif d[i, j] == 'O':
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1]
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1] + 1
            dI[i, j] = dI[i-1, j] + dI[i, j-1] - dI[i-1, j-1]
        else:
            dJ[i, j] = dJ[i-1, j] + dJ[i, j-1] - dJ[i-1, j-1]
            dO[i, j] = dO[i-1, j] + dO[i, j-1] - dO[i-1, j-1]
            dI[i, j] = dI[i-1, j] + dI[i, j-1] - dI[i-1, j-1] + 1

for _ in range(K):
    b = map(int, raw_input().split())
    # D = {}
    # D['J'] = D['O'] = D['I'] = 0
    # for i in range(b[0], b[2]+1):
        # for j in range(b[1], b[3]+1):
            # D[d[i, j]] += 1
    # print D['J'], D['O'], D['I']
    print dJ[b[2], b[3]]-dJ[b[0]-1, b[3]]-dJ[b[2], b[1]-1]+dJ[b[0]-1, b[1]-1], dO[b[2], b[3]]-dO[b[0]-1, b[3]]-dO[b[2], b[1]-1]+dO[b[0]-1, b[1]-1], dI[b[2], b[3]]-dI[b[0]-1, b[3]]-dI[b[2], b[1]-1]+dI[b[0]-1, b[1]-1]