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("") }

d = m*n+1

orig = a.map{|r| r.map{|c|
	case c
		when ?J; 1
		when ?O; d
		when ?I; d * d
	end
}}

accum = orig.map{|r| [0] + r.accum}
accum_2d = accum.transpose.map{|r| [0] + r.accum}.transpose

k.times{
	y1, x1, y2, x2 = gets.split.map &:to_i
	x = accum_2d[y2][x2] - accum_2d[y1-1][x2] - accum_2d[y2][x1-1] + accum_2d[y1-1][x1-1]
	j = x % d
	x /= d
	o = x % d
	i = x / d
	puts "#{j} #{o} #{i}"
}