#include <bits/stdc++.h>
using namespace std;
int map_j[1010][1010];
int map_o[1010][1010];
int map_i[1010][1010];
int main() {
  int h, w;
  long long int kk;
  cin >> h >> w;
  cin >> kk;
  string str;
  vector<string> joi;
  for (int i = 0; i < (int)(h); i++) {
    cin >> str;
    joi.push_back(str);
  }
  string hit = "JOI";
  int tmp = 0;
  for (int j = (int)(1); j < (int)(w + 2); j++) {
    if (joi[0][j - 1] == hit[0]) tmp++;
    map_j[1][j] = tmp;
  }
  for (int j = (int)(2); j < (int)(h + 2); j++) {
    tmp = 0;
    for (int k = (int)(1); k < (int)(w + 2); k++) {
      if (joi[j - 1][k - 1] == hit[0]) tmp++;
      map_j[j][k] = map_j[j - 1][k] + tmp;
    }
  }
  for (int j = (int)(1); j < (int)(w + 2); j++) {
    if (joi[0][j - 1] == hit[1]) tmp++;
    map_o[1][j] = tmp;
  }
  for (int j = (int)(2); j < (int)(h + 2); j++) {
    tmp = 0;
    for (int k = (int)(1); k < (int)(w + 1); k++) {
      if (joi[j - 1][k - 1] == hit[1]) tmp++;
      map_o[j][k] = map_o[j - 1][k] + tmp;
    }
  }
  for (int j = (int)(1); j < (int)(w + 2); j++) {
    if (joi[0][j - 1] == hit[2]) tmp++;
    map_i[1][j] = tmp;
  }
  for (int j = (int)(2); j < (int)(h + 2); j++) {
    tmp = 0;
    for (int k = (int)(1); k < (int)(w + 2); k++) {
      if (joi[j - 1][k - 1] == hit[2]) tmp++;
      map_i[j][k] = map_i[j - 1][k] + tmp;
    }
  }
  for (int i = 0; i < (int)(kk); i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int t[3];
    t[0] = map_j[x2][y2] + map_j[x1 - 1][y1 - 1] - map_j[x1 - 1][y2] -
           map_j[x2][y1 - 1];
    t[1] = map_o[x2][y2] + map_o[x1 - 1][y1 - 1] - map_o[x1 - 1][y2] -
           map_o[x2][y1 - 1];
    t[2] = map_i[x2][y2] + map_i[x1 - 1][y1 - 1] - map_i[x1 - 1][y2] -
           map_i[x2][y1 - 1];
    cout << t[0] << " " << t[1] << " " << t[2] << endl;
  }
  return 0;
}
