#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e18 + 9;
const long long int Mod = 1e9 + 7;
inline long long int replac(string s) {
  double ans = 0;
  for (long long int i = 0; i < s.length(); i++) {
    ans += (s[i] - '0') * pow(10, s.length() - i - 1);
  }
  return (long long int)ans;
}
inline string numstr(long long int m) {
  string s = "";
  while (m > 0) {
    char c;
    long long int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  string st = "";
  for (long long int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
char a[1001][1001];
long long int b[3][1001][1001];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  long long int n, m, t;
  cin >> n >> m >> t;
  for (long long int i = 0; i < 1001; i++) {
    for (long long int j = 0; j < 1001; j++) {
      for (long long int k = 0; k < 3; k++) b[k][i][j] = 0;
    }
  }
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j = 1; j < m + 1; j++) {
      char c;
      cin >> c;
      if (c == 'J') {
        b[0][i][j] = 1;
      } else if (c == 'O') {
        b[1][i][j] = 1;
      } else {
        b[2][i][j] = 1;
      }
    }
  }
  for (long long int i = 1; i < n + 1; i++) {
    for (long long int j = 1; j < m + 1; j++) {
      for (long long int k = 0; k < 3; k++) {
        b[k][i][j] += (b[k][i - 1][j] + b[k][i][j - 1] - b[k][i - 1][j - 1]);
      }
      cout << b[0][i][j];
    }
    cout << endl;
  }
  for (long long int i = 0; i < t; i++) {
    long long int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    long long int d = 0;
    for (long long int j = 0; j < 3; j++) {
      d = b[j][x2][y2];
      d += b[j][x1 - 1][y1 - 1];
      d -= b[j][x1 - 1][y2];
      d -= b[j][x2][y1 - 1];
      if (j != 2) cout << d << " ";
      d = 0;
    }
    cout << endl;
  }
  return 0;
}
