#include <bits/stdc++.h>
using namespace std;
int n, m, k;
string s[1100];
int table[1100][1100][10];
int a[4];
int main() {
  cin >> m >> n >> k;
  for (long long i = 0; i < (m); i++) {
    cin >> s[i];
    for (long long j = 0; j < (n); j++) {
      if (s[i][j] == 'J') table[i][j][0] = 1;
      if (s[i][j] == 'O') table[i][j][1] = 1;
      if (s[i][j] == 'I') table[i][j][2] = 1;
    }
  }
  for (long long i = 0; i < (m); i++) {
    for (long long j = (1); j < (n); j++) {
      table[i][j][0] += table[i][j - 1][0];
      table[i][j][1] += table[i][j - 1][1];
      table[i][j][2] += table[i][j - 1][2];
    }
  }
  for (long long i = (1); i < (m); i++) {
    for (long long j = 0; j < (n); j++) {
      table[i][j][0] += table[i - 1][j][0];
      table[i][j][1] += table[i - 1][j][1];
      table[i][j][2] += table[i - 1][j][2];
    }
  }
  for (long long i = 0; i < (3); i++) {
    for (long long j = 0; j < (m); j++) {
      for (long long k = 0; k < (n); k++) {
        cout << table[j][k][i] << " ";
      }
      cout << endl;
    }
    cout << endl;
  }
  for (long long i = 0; i < (k); i++) {
    for (long long i = 0; i < (4); i++) {
      cin >> a[i];
      a[i]--;
    }
    for (long long i = 0; i < (3); i++) {
      int num = table[a[2]][a[3]][i];
      if (a[1] > 0) num -= table[a[2]][a[1] - 1][i];
      if (a[0] > 0) num -= table[a[0] - 1][a[3]][i];
      if (a[1] > 0 && a[0] > 0) num += table[a[0] - 1][a[1] - 1][i];
      if (i == 0)
        cout << num;
      else
        cout << " " << num;
    }
    cout << endl;
  }
}
