class Array
	def accum
		inject([0]){|a, x| a + [a[-1] + x]}[1..-1]
	end
end

m, n = gets.split.map &:to_i
k = gets.to_i

a = (1..m).map{ gets.chomp.split("") }

zero_one = a.map{|r| r.map{|c| c == ?J ? 1 : 0}}
accum = zero_one.map{|r| [0] + r.accum}
accum_j = accum.transpose.map{|r| [0] + r.accum}.transpose

zero_one = a.map{|r| r.map{|c| c == ?O ? 1 : 0}}
accum = zero_one.map{|r| [0] + r.accum}
accum_o = accum.transpose.map{|r| [0] + r.accum}.transpose

zero_one = a.map{|r| r.map{|c| c == ?I ? 1 : 0}}
accum = zero_one.map{|r| [0] + r.accum}
accum_i = accum.transpose.map{|r| [0] + r.accum}.transpose

k.times{
	y1, x1, y2, x2 = gets.split.map &:to_i
	j = accum_j[y2][x2] - accum_j[y1-1][x2] - accum_j[y2][x1-1] + accum_j[y1-1][x1-1]
	o = accum_o[y2][x2] - accum_o[y1-1][x2] - accum_o[y2][x1-1] + accum_o[y1-1][x1-1]
	i = accum_i[y2][x2] - accum_i[y1-1][x2] - accum_i[y2][x1-1] + accum_i[y1-1][x1-1]
	puts "#{j} #{o} #{i}"
}