import Control.Applicative ((<$>), (<*>))
import Control.Monad (replicateM, forM_)
import Data.Vector (Vector, (!), (//))
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM

main :: IO ()
main = do
  [m, n] <- map read <$> words <$> getLine
  k <- readLn
  ss <- replicateM m getLine
  solve m n ss <$> replicateM k (map read <$> words <$> getLine) >>= mapM_ printJoi

solve :: Int -> Int -> [String] -> [[Int]] -> [JOI]
solve m n ss qs = map f qs
  where s00 = (head . head) ss
        dp :: Vector JOI
        dp = V.create $ do
          v <- VM.replicate ((m+1) * (n+1)) (0, 0, 0)
          forM_ (zip [1..] ss) $ \(i, s) -> do
            forM_ (zip [1..] s) $ \(j, c) -> do
              l <- VM.read v ((n+1)*i+j-1)
              u <- VM.read v ((n+1)*i+j-n-1)
              ul <- VM.read v ((n+1)*i+j-n-2)
              VM.write v ((n+1)*i+j) (addJoi (subJoi (addJoi u l) ul) (toJoi c))
          return v
        f [a, b, c, d] = addJoi (subJoi (dp ! (c*(n+1)+d)) (addJoi (dp ! (c*(n+1)+(b-1))) (dp ! ((a-1)*(n+1)+d)))) (dp ! ((a-1)*(n+1)+(b-1)))
        
type JOI = (Int, Int, Int)

addJoi :: JOI -> JOI -> JOI
addJoi (j1, o1, i1) (j2, o2, i2) = (j1+j2, o1+o2, i1+i2)

subJoi :: JOI -> JOI -> JOI
subJoi (j1, o1, i1) (j2, o2, i2) = (j1-j2, o1-o2, i1-i2)

toJoi :: Char -> JOI
toJoi 'J' = (1,0,0)
toJoi 'O' = (0,1,0)
toJoi 'I' = (0,0,1)

printJoi :: JOI -> IO ()
printJoi (j, o, i) = putStrLn . unwords . map show $ [j, o, i]

