#include <bits/stdc++.h>
using namespace std;
int m, n;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
char mas[1001][1001];
char test[1001][1001];
int sx, sy, gx, gy;
int J, O, I;
void dfs(int x, int y) {
  if (mas[x][y] == 'J') {
    J++;
  } else if (mas[x][y] == 'O') {
    O++;
  } else if (mas[x][y] == 'I') {
    I++;
  }
  if (x == gx && y == gy) return;
  for (int i = 0; i < 4; i++) {
    int nx = dx[i] + x;
    int ny = dy[i] + y;
    if (sx <= nx && nx <= gx && sy <= ny && ny <= gy && test[nx][ny] == '1') {
      test[nx][ny] = '0';
      dfs(nx, ny);
    }
  }
}
int main() {
  cin >> m >> n;
  int k;
  cin >> k;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      cin >> mas[i][j];
    }
  }
  for (int i = 0; i < k; i++) {
    cin >> sx >> sy >> gx >> gy;
    sx--;
    sy--;
    gx--;
    gy--;
    memset(test, '1', sizeof(test));
    J = 0;
    O = 0;
    I = 0;
    test[sx][sy] = 0;
    dfs(sx, sy);
    cout << J << " " << O << " " << I << endl;
  }
}
