def solve():

  m, n = list(map(int,input().split()))

  k = int(eval(input()))

  mp = [eval(input()) for i in range(m)]

  dp = [[[0] * 3 for j in range(n + 1)] for i in range(m + 1)]

  dic = {"J":0, "O":1, "I":2}



  for i in range(1, m + 1):

    dpi = dp[i]

    dpi_1 = dp[i - 1]



    for j in range(1, n + 1):

      dpij = dpi[j]

      dpij_1 = dpi[j - 1]

      dpi_1j = dpi_1[j]

      dpi_1j_1 = dpi_1[j - 1]



      for p in range(3):

        #dp[i][j][p] = dp[i][j - 1][p] + dp[i - 1][j][p] - dp[i - 1][j - 1][p]

        dpij[p] = dpij_1[p] + dpi_1j[p] - dpi_1j_1[p]



      #dp[i][j][mp[i - 1][j - 1]] += 1

      dpij[dic[mp[i - 1][j - 1]]] += 1

     



  for i in range(k):

    x1,y1,x2,y2 = list(map(int,input().split()))

    dpx2 = dp[x2]

    dpx1 = dp[x1 - 1]

    dpx2y2 = dpx2[y2]

    dpx2y1 = dpx2[y1 - 1]

    dpx1y2 = dpx1[y2]

    dpx1y1 = dpx1[y1 - 1]

    #print(dp[x2][y2][0] - dp[x1 - 1][y2][0] - dp[x2][y1 - 1][0] + dp[x1 - 1][y1 - 1][0], dp[x2][y2][1] - dp[x1 - 1][y2][1] - dp[x2][y1 - 1][1] + dp[x1 - 1][y1 - 1][1], dp[x2][y2][2] - dp[x1 - 1][y2][2] - dp[x2][y1 - 1][2] + dp[x1 - 1][y1 - 1][2])

    print((dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0], dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1], dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2]))

solve()

def solve():

  m, n = list(map(int,input().split()))

  k = int(eval(input()))

  mp = [eval(input()) for i in range(m)]

  dp = [[[0] * 3 for j in range(n + 1)] for i in range(m + 1)]

  dic = {"J":0, "O":1, "I":2}



  for i in range(1, m + 1):

    dpi = dp[i]

    dpi_1 = dp[i - 1]



    for j in range(1, n + 1):

      dpij = dpi[j]

      dpij_1 = dpi[j - 1]

      dpi_1j = dpi_1[j]

      dpi_1j_1 = dpi_1[j - 1]



      for p in range(3):

        #dp[i][j][p] = dp[i][j - 1][p] + dp[i - 1][j][p] - dp[i - 1][j - 1][p]

        dpij[p] = dpij_1[p] + dpi_1j[p] - dpi_1j_1[p]



      #dp[i][j][mp[i - 1][j - 1]] += 1

      dpij[dic[mp[i - 1][j - 1]]] += 1

     

  ans = ""

  for i in range(k):

    x1,y1,x2,y2 = list(map(int,input().split()))

    dpx2 = dp[x2]

    dpx1 = dp[x1 - 1]

    dpx2y2 = dpx2[y2]

    dpx2y1 = dpx2[y1 - 1]

    dpx1y2 = dpx1[y2]

    dpx1y1 = dpx1[y1 - 1]

    #print(dp[x2][y2][0] - dp[x1 - 1][y2][0] - dp[x2][y1 - 1][0] + dp[x1 - 1][y1 - 1][0], dp[x2][y2][1] - dp[x1 - 1][y2][1] - dp[x2][y1 - 1][1] + dp[x1 - 1][y1 - 1][1], dp[x2][y2][2] - dp[x1 - 1][y2][2] - dp[x2][y1 - 1][2] + dp[x1 - 1][y1 - 1][2])

    ans += str(dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0]) + " " + str(dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1]) + " " + str(dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2]) + "\n"

  ans = ans.rstrip()

  print(ans)





solve()

def solve():
    m, n = list(map(int, input().split()))
    k = int(eval(input()))
    mp = [eval(input()) for i in range(m)]
    dp = [[[0] * 3 for j in range(n + 1)] for i in range(m + 1)]
    dic = {"J": 0, "O": 1, "I": 2}
    for i in range(1, m + 1):
        dpi = dp[i]
        dpi_1 = dp[i - 1]
        for j in range(1, n + 1):
            dpij = dpi[j]
            dpij_1 = dpi[j - 1]
            dpi_1j = dpi_1[j]
            dpi_1j_1 = dpi_1[j - 1]
            for p in range(3):
                # dp[i][j][p] = dp[i][j - 1][p] + dp[i - 1][j][p] - dp[i - 1][j - 1][p]
                dpij[p] = dpij_1[p] + dpi_1j[p] - dpi_1j_1[p]
            # dp[i][j][mp[i - 1][j - 1]] += 1
            dpij[dic[mp[i - 1][j - 1]]] += 1
    for i in range(k):
        x1, y1, x2, y2 = list(map(int, input().split()))
        dpx2 = dp[x2]
        dpx1 = dp[x1 - 1]
        dpx2y2 = dpx2[y2]
        dpx2y1 = dpx2[y1 - 1]
        dpx1y2 = dpx1[y2]
        dpx1y1 = dpx1[y1 - 1]
        # print(dp[x2][y2][0] - dp[x1 - 1][y2][0] - dp[x2][y1 - 1][0] + dp[x1 - 1][y1 - 1][0], dp[x2][y2][1] - dp[x1 - 1][y2][1] - dp[x2][y1 - 1][1] + dp[x1 - 1][y1 - 1][1], dp[x2][y2][2] - dp[x1 - 1][y2][2] - dp[x2][y1 - 1][2] + dp[x1 - 1][y1 - 1][2])
        print(
            (
                dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0],
                dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1],
                dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2],
            )
        )


solve()

def solve():
    m, n = list(map(int, input().split()))
    k = int(eval(input()))
    mp = [eval(input()) for i in range(m)]
    dp = [[[0] * 3 for j in range(n + 1)] for i in range(m + 1)]
    dic = {"J": 0, "O": 1, "I": 2}
    for i in range(1, m + 1):
        dpi = dp[i]
        dpi_1 = dp[i - 1]
        for j in range(1, n + 1):
            dpij = dpi[j]
            dpij_1 = dpi[j - 1]
            dpi_1j = dpi_1[j]
            dpi_1j_1 = dpi_1[j - 1]
            for p in range(3):
                # dp[i][j][p] = dp[i][j - 1][p] + dp[i - 1][j][p] - dp[i - 1][j - 1][p]
                dpij[p] = dpij_1[p] + dpi_1j[p] - dpi_1j_1[p]
            # dp[i][j][mp[i - 1][j - 1]] += 1
            dpij[dic[mp[i - 1][j - 1]]] += 1
    ans = ""
    for i in range(k):
        x1, y1, x2, y2 = list(map(int, input().split()))
        dpx2 = dp[x2]
        dpx1 = dp[x1 - 1]
        dpx2y2 = dpx2[y2]
        dpx2y1 = dpx2[y1 - 1]
        dpx1y2 = dpx1[y2]
        dpx1y1 = dpx1[y1 - 1]
        # print(dp[x2][y2][0] - dp[x1 - 1][y2][0] - dp[x2][y1 - 1][0] + dp[x1 - 1][y1 - 1][0], dp[x2][y2][1] - dp[x1 - 1][y2][1] - dp[x2][y1 - 1][1] + dp[x1 - 1][y1 - 1][1], dp[x2][y2][2] - dp[x1 - 1][y2][2] - dp[x2][y1 - 1][2] + dp[x1 - 1][y1 - 1][2])
        ans += (
            str(dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0])
            + " "
            + str(dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1])
            + " "
            + str(dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2])
            + "\n"
        )
    ans = ans.rstrip()
    print(ans)


solve()

+    ans = "" -        print( -            ( -                dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0], -                dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1], -                dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2], -            ) +        ans += ( +            str(dpx2y2[0] - dpx1y2[0] - dpx2y1[0] + dpx1y1[0]) +            + " " +            + str(dpx2y2[1] - dpx1y2[1] - dpx2y1[1] + dpx1y1[1]) +            + " " +            + str(dpx2y2[2] - dpx1y2[2] - dpx2y1[2] + dpx1y1[2]) +            + "\n" +    ans = ans.rstrip() +    print(ans)