#include <bits/stdc++.h>
using namespace std;
string s[9999];
int h, w, dp[1002][10002][3], a, b, c, d, n;
int main() {
  cin >> h >> w >> n;
  for (int i = 0; i < w + 1; i++) s[0] += '@';
  for (int i = 0; i < h; i++) cin >> s[i + 1], s[i + 1] = '@' + s[i + 1];
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++) {
      for (int k = 0; k < 3; k++) dp[i][j][k] += dp[i][j - 1][k];
      if (s[i][j] == 'J') dp[i][j][0]++;
      if (s[i][j] == 'O') dp[i][j][1]++;
      if (s[i][j] == 'I') dp[i][j][2]++;
    }
  for (int i = 1; i <= h; i++)
    for (int j = 1; j <= w; j++)
      for (int k = 0; k < 3; k++) dp[i][j][k] += dp[i - 1][j][k];
  for (int i = 0; i < h + 1; i++) {
    for (int j = 0; j < w + 1; j++) cout << dp[i][j][0] << ' ';
    cout << endl;
  }
  while (n--) {
    cin >> a >> b >> c >> d;
    a--;
    b--;
    cout << dp[c][d][0] - dp[a][d][0] - dp[c][b][0] + dp[a][b][0] << ' ';
    cout << dp[c][d][1] - dp[a][d][1] - dp[c][b][1] + dp[a][b][1] << ' ';
    cout << dp[c][d][2] - dp[a][d][2] - dp[c][b][2] + dp[a][b][2] << endl;
  }
}
