#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
typedef pair<int, int> pii;
static const int INF = 1LL<<61;

int m, n, k, joi[3][1005][1005];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> m >> n >> k;

  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      char c;
      cin >> c;
      int typ;
      if (c == 'J')
	typ = 0;
      else if (c == 'O')
	typ = 1;
      else
	typ = 2;

      for (int i2 = 0; i2 < 3; ++i2) {
	joi[i2][j][i] += joi[i2][j - 1][i];
      }
      joi[typ][j][i]++;
    }
  }

  for (int j = 1; j <= n; ++j) {
    for (int i = 1; i <= m; ++i) {
      for (int k = 0; k < 3; ++k) {
	joi[k][j][i] += joi[k][j][i - 1];
      }
    }
  }

  for (int i = 0; i < k; ++i) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    for (int j = 0; j < 3; ++j) {
      int ans = joi[j][d][c] - joi[j][b - 1][c] - joi[j][d][a - 1] + joi[j][b - 1][a - 1];
      cout << ans;
      if (j != 2) cout << " ";
    }
    cout << endl;
  }
  
  return 0;
}