#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

char t0[1000][1000];
int t10[100][100][4] = {{0},{0},{0}};


int main(){
  int n, m, k;
  cin >> m >> n;
  cin >> k;
  for(int i = 0; i < m; i++){
    scanf("%s", t0[i]);
  }
  for(int i = 0; i < k; i++){
    int x1, y1, x2, y2, xx1, xx2, yy1, yy2, j = 0, o = 0, i = 0;
    cin >> y1 >> x1 >> y2 >> x2;
    x1--; y1--; x2--; y2--;
    if(x1 > 20 &&  x2 < n - 20 && x2 - x1 > 20 && y1 > 20 &&  y2 < m - 20 && y2 - y1 > 20){
    xx1 = x1 / 10;
    xx2 = x2 / 10 + 1;
    yy1 = y1 / 10;
    yy2 = y2 / 10 + 1;
    for(int x = xx1 + 1; x < xx2; x++){
      for(int y = yy1 + 1; y < yy2; y++){
        if(t10[y][x][0]){
          j += t10[y][x][1];
          o += t10[y][x][2];
          i += t10[y][x][3];
        }else{
          int jj = 0, oo = 0, ii= 0;
          for(int xx = 0; xx < 10; xx++){
            for(int yy = 0; yy < 10; yy++){
              char cc = t0[y * 10 + yy][x * 10 + xx];
              if(cc == 'J')
                jj++;
              else if(cc == 'O')
                oo++;
              else
                ii++;
            }
          }
          t10[y][x][0] = 1;
          t10[y][x][1] = jj;
          t10[y][x][2] = ii;
          t10[y][x][3] = oo;
          j += jj;
          o += oo;
          i += ii;
        }
      }
    }
    
    for(int x = x1; x <= xx1; x++){
      for(int y = y1; y <= y2; y++){
        char c = t0[y][x];
        //printf("(%d:%d:%c)", x, y, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }
    for(int x = xx2; x <= x2; x++){
      for(int y = y1; y <= y2; y++){
        char c = t0[y][x];
        //printf("(%d:%d:%c)", x, y, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }

    for(int x = xx1 + 1; x < xx2; x++){
      for(int y = y1; y <= yy1; y++){
        char c = t0[y][x];
        //printf("(%d:%d:%c)", x, y, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }
    for(int x = xx1 + 1; x < xx2; x++){
      for(int y = yy2; y <= y2; y++){
        char c = t0[y][x];
        //printf("(%d:%d:%c)", x, y, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }
  //printf("%d", 1000000);

    }else{

    for(int x = x1; x <= x2; x++){
      for(int y = y1; y <= y2; y++){
        char c = t0[y][x];
        //printf("(%d:%d:%c)", x + 1, y + 1, c);
        if(c == 'J')
          j++;
        else if(c == 'O')
          o++;
        else
          i++;
      }
      //printf("\n");
    }
  }

    printf("%d %d %d\n", j, o, i);
  }
  return 0;
}