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
}}

orig.each{|r|
	(1..n-1).each{|j|
		r[j] += r[j-1]
	}
	r.unshift 0
}

(1..m-1).each{|i|
	(1..n).each{|j|
		orig[i][j] += orig[i-1][j]
	}
}
orig.unshift([0] * (n+1))

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