fun main(args:Array<String>?): Unit {
    val (m, n) = readLine()!!.trim().split(' ').map(String::toInt)
    val k = readLine()!!.trim().toInt()
    val state = Array(m){readLine()!!.trim()}
    val jungle = state.map{line -> line.map{if (it == 'J') 1 else 0}.toIntArray()}
    val ocean = state.map{line -> line.map{if (it == 'O') 1 else 0}.toIntArray()}
    val ice = state.map{line -> line.map{if (it == 'I') 1 else 0}.toIntArray()}
    for (i in 0 until m){
        for (j in 0 until n){
            if (i > 0) {
                jungle[i][j] += jungle[i - 1][j]
                ocean[i][j] += ocean[i - 1][j]
                ice[i][j] += ice[i - 1][j]
            }
            if (j > 0){
                jungle[i][j] += jungle[i][j - 1]
                ocean[i][j] += ocean[i][j - 1]
                ice[i][j] += ice[i][j - 1]
            }
            if (i > 0 && j > 0){
                jungle[i][j] -= jungle[i - 1][j - 1]
                ocean[i][j] -= ocean[i - 1][j - 1]
                ice[i][j] -= ice[i - 1][j - 1]
            }
        }
    }
    fun count(a: Int, b: Int, c: Int, d: Int, dp: List<IntArray>): Int {
        var result = dp[c][d]
        if (a > 0) result -= dp[a - 1][d]
        if (b > 0) result -= dp[c][b - 1]
        if (a > 0 && b > 0) result += dp[a - 1][b - 1]
        return result
    }
    repeat(k){
        val (a, b, c, d) = readLine()!!.trim().split(' ').map{it.toInt() - 1}
        println("${count(a, b, c, d, jungle)} ${count(a, b, c, d, ocean)} ${count(a, b, c, d, ice)}")
    }
}
