#include <bits/stdc++.h>
using namespace std;
int m, n, k, imos[3][1001][1001];
string s[1000];
int main() {
  cin >> m >> n >> k;
  for (int i = 1; i <= m; i++) {
    cin >> s[i];
    for (int j = 0; j < n; j++) {
      int hoge;
      if (s[i][j] == 'J') hoge = 0;
      if (s[i][j] == 'O') hoge = 1;
      if (s[i][j] == 'I') hoge = 2;
      imos[0][i][j + 1] = imos[0][i][j];
      imos[1][i][j + 1] = imos[1][i][j];
      imos[2][i][j + 1] = imos[2][i][j];
      imos[hoge][i][j + 1]++;
    }
  }
  for (int i = 0; i < 3; i++) {
    for (int j = 1; j <= m; j++) {
      for (int h = 1; h <= n; h++) {
        imos[i][j][h] += imos[i][j - 1][h];
      }
    }
  }
  for (int i = 0; i < k; i++) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    for (int j = 0; j < 3; j++) {
      cout << imos[j][c][d] - imos[j][a - 1][d] - imos[j][c][b - 1] +
                  imos[j][a - 1][b - 1]
           << " ";
    }
    cout << endl;
  }
  return 0;
}
