m, n = gets.split.map(&:to_i)
k = gets.to_i
joi = Array.new(m)
m.times do |i|
  joi[i] = gets.chomp
end
js = Array.new(m){ Array.new(n, 0) }
os = Array.new(m){ Array.new(n, 0) }
is = Array.new(m){ Array.new(n, 0) }
m.times do |i|
  n.times do |j|
    if joi[i][j] == 'J' then js[i][j] = 1
    elsif joi[i][j] == 'O' then os[i][j] = 1
    else is[i][j] = 1
    end
  end
end
m.times do |i|
  (1...n).each do |j|
    js[i][j] += js[i][j-1]
    os[i][j] += os[i][j-1]
    is[i][j] += is[i][j-1]
  end
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 += b > 1 ? js[i][d-1] - js[i][b-2] : js[i][d-1]
    oc += b > 1 ? os[i][d-1] - os[i][b-2] : os[i][d-1]
    ic += b > 1 ? is[i][d-1] - is[i][b-2] : is[i][d-1]
  end
  puts "#{jc} #{oc} #{ic}"
end