#include <bits/stdc++.h>
using namespace std;
static const double PI = acos(-1.0);
static const double EPS = 1e-9;
int m, n, k;
vector<string> field;
int sum[3][1024][1024];
int main() {
  cin >> m >> n >> k;
  for (int i = 0; i < m; ++i) {
    string s;
    cin >> s;
    field.push_back(s);
  }
  memset(sum, 0, sizeof(sum));
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      for (int a = 0; a <= i; ++a) {
        for (int b = 0; b <= j; ++b) {
          int t = (field[a][b] == 'J') ? 0 : ((field[a][b] == 'O') ? 1 : 2);
          sum[t][i][j]++;
        }
      }
    }
  }
  for (int i = 0; i < k; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    a--;
    b--;
    c--;
    d--;
    for (int t = 0; t < 3; ++t) {
      int w, x, y, z;
      w = sum[t][c][d];
      x = (a != 0 && b != 0 ? sum[t][a - 1][b - 1] : 0);
      y = (a != 0 ? sum[t][a - 1][d] : 0);
      z = (b != 0 ? sum[t][c][b - 1] : 0);
      cout << w + x - y - z << (t != 2 ? ' ' : '\n');
    }
    cout << flush;
  }
  return 0;
}
