#include <bits/stdc++.h>
int m, n, k;
int jh[1000][1000] = {}, jw[1000][1000] = {}, oh[1000][1000] = {},
    ow[1000][1000] = {}, ih[1000][1000] = {}, iw[1000][1000] = {};
char field[1000][1000];
void solve_ac(int a, int b, int c, int d) {
  int j = 0, o = 0, i = 0;
  if (a == 0) {
    for (int lp = b; lp <= d; lp++) {
      j += jw[lp][c];
      o += ow[lp][c];
      i += iw[lp][c];
    }
  } else {
    for (int lp = b; lp <= d; lp++) {
      j += jw[lp][c] - jw[lp][a - 1];
      o += ow[lp][c] - ow[lp][a - 1];
      i += iw[lp][c] - iw[lp][a - 1];
    }
  }
  printf("%d %d %d\n", j, o, i);
}
void solve_bd(int a, int b, int c, int d) {
  int j = 0, o = 0, i = 0;
  if (b == 0) {
    for (int lp = a; lp <= c; lp++) {
      j += jh[lp][d];
      o += oh[lp][d];
      i += ih[lp][d];
    }
  } else {
    for (int lp = a; lp <= c; lp++) {
      j += jh[lp][d] - jh[lp][b - 1];
      o += oh[lp][d] - oh[lp][b - 1];
      i += ih[lp][d] - ih[lp][b - 1];
    }
  }
  printf("%d %d %d\n", j, o, i);
}
int main(void) {
  int a, b, c, d;
  char temp;
  scanf("%d %d %d", &m, &n, &k);
  for (int lp_h = 0; lp_h < m; lp_h++) {
    for (int lp_w = 0; lp_w < n; lp_w++) {
      scanf("%c", &temp);
      if (temp == '\n') {
        lp_w--;
      } else {
        field[lp_h][lp_w] = temp;
      }
    }
  }
  for (int lp_h = 0; lp_h < m; lp_h++) {
    if (field[lp_h][0] == 'J') {
      jh[lp_h][0] = 1;
    } else if (field[lp_h][0] == 'O') {
      oh[lp_h][0] = 1;
    } else {
      ih[lp_h][0] = 1;
    }
    for (int lp_w = 1; lp_w < n; lp_w++) {
      if (field[lp_h][lp_w] == 'J') {
        jh[lp_h][lp_w] = 1;
      } else if (field[lp_h][lp_w] == 'O') {
        oh[lp_h][lp_w] = 1;
      } else {
        ih[lp_h][lp_w] = 1;
      }
      jh[lp_h][lp_w] += jh[lp_h][lp_w - 1];
      oh[lp_h][lp_w] += oh[lp_h][lp_w - 1];
      ih[lp_h][lp_w] += ih[lp_h][lp_w - 1];
    }
  }
  for (int lp_w = 0; lp_w < n; lp_w++) {
    if (field[0][lp_w] == 'J') {
      jw[0][lp_w] = 1;
    } else if (field[0][lp_w] == 'O') {
      ow[0][lp_w] = 1;
    } else {
      iw[0][lp_w] = 1;
    }
    for (int lp_h = 1; lp_h < m; lp_h++) {
      if (field[lp_h][lp_w] == 'J') {
        jw[lp_h][lp_w] = 1;
      } else if (field[lp_h][lp_w] == 'O') {
        ow[lp_h][lp_w] = 1;
      } else {
        iw[lp_h][lp_w] = 1;
      }
      jw[lp_h][lp_w] += jw[lp_h - 1][lp_w];
      ow[lp_h][lp_w] += ow[lp_h - 1][lp_w];
      iw[lp_h][lp_w] += iw[lp_h - 1][lp_w];
    }
  }
  for (int lp = 0; lp < k; lp++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    if (c - a > d - b) {
      solve_ac(a - 1, b - 1, c - 1, d - 1);
    } else {
      solve_bd(a - 1, b - 1, c - 1, d - 1);
    }
  }
  return 0;
  return 0;
}
