m, n = (int(x) for x in input().split())
k = int(input())
joi_list = [0]*m
for i in range(m):
  joi_list[i] = list(input())
c = ['J', 'O', 'I']
t = [[[0]*(n+1) for i in range(m+1)] for j in range(len(c))]

for h in range(len(c)):
  for i in range(1,m+1):
    co = 0
    for j in range(n):
      if joi_list[i-1][j] == c[h]:
        co += 1
      t[h][i][j+1] = t[h][i-1][j+1] + co

ans = [0]*len(c)
for i in range(k):
  p1, q1, p2, q2 = (int (x) for x in input().split())
  for h in range(len(c)):
    ans[h] = t[h][p2][q2]-t[h][p2][q1-1]-t[h][p1-1][q2]+t[h][p1-1][q1-1]
  print(*ans)
