h, w = gets.chomp.split.map(&:to_i)
k = gets.to_i
cum = Array.new(h + 1){[]}
cum[0] = Array.new(w + 1){[0, 0, 0]}
h.times{|i|
    line = gets.chomp
    temp = [[0, 0, 0]]
    w.times{|j|
        temp[j + 1] = temp[j].dup
        kind = (line[j] == 'J' ? 0 : (line[j] == 'O' ? 1 : 2))
        temp[j + 1][kind] += 1
    }
    temp.each_with_index{|ar, index|
        cum[i + 1][index] = cum[i][index].dup
        3.times{|j| cum[i + 1][index][j] += ar[j]}
    }
}
k.times{
    a, b, c, d = gets.chomp.split.map(&:to_i)
    temp = cum[c][d].dup
    3.times{|j| temp[j] -= cum[a - 1][d][j]}
    3.times{|j| temp[j] -= cum[c][b - 1][j]}
    3.times{|j| temp[j] += cum[a - 1][b - 1][j]}
    puts temp.join(" ")
}

