m,n = gets.split.map(&:to_i)
k = gets.to_i
f = m.times.map{gets.chomp.split("")}
tar = k.times.map{gets.split.map(&:to_i)}

nm = ["J","O","I"]
cnt = Array.new(3).map{Array.new(m+1).map{Array.new(n+1,0)}}
for i in 0..(m-1)
  for j in 0..(n-1)
    for id in 0..2
      f[i][j] == nm[id] ? pl = 1 : pl = 0
      cnt[id][i+1][j+1] = cnt[id][i+1][j] + pl
    end
  end
end

for id in 0..2
  for i in 1..m
    for j in 1..n
      cnt[id][i][j] += cnt[id][i-1][j]
    end
  end
end

for i in 0..(k-1)
  ans = Array.new(3,0)
  p = tar[i]
  for id in 0..2
    ans[id] = cnt[id][p[2]][p[3]]-cnt[id][p[0]-1][p[3]]-cnt[id][p[2]][p[1]-1]+cnt[id][p[0]-1][p[1]-1]
  end
  puts ans.join(" ")
end
