m, n = gets.split.map(&:to_i)
k = gets.to_i
joi = Array.new(m)
m.times do |i|
  joi[i] = gets.chomp
end
k.times do
  a, b, c, d = gets.split.map(&:to_i)
  jc, oc, ic = 0, 0, 0
  (a-1..c-1).each do |i|
     jc += joi[i][b-1..d-1].count('J')
     oc += joi[i][b-1..d-1].count('O')
     ic += joi[i][b-1..d-1].count('I')
  end
  puts "#{jc} #{oc} #{ic}"
end