#include<iostream>
#include<algorithm>

using namespace std;

const int MAX = 1001;

int sumj[MAX][MAX],sumo[MAX][MAX], sumi[MAX][MAX];
string data[MAX];
int x,y;

void make(){
  for(int i = 1; i <= y; i++){
    for(int j = 1; j <= x; j++){
      sumj[i][j] = sumj[i-1][j]+sumj[i][j-1]-sumj[i-1][j-1];
      sumo[i][j] = sumo[i-1][j]+sumo[i][j-1]-sumo[i-1][j-1];
      sumi[i][j] = sumi[i-1][j]+sumi[i][j-1]-sumi[i-1][j-1];
      if(data[i][j] == 'J') sumj[i][j]++;
      if(data[i][j] == 'O') sumo[i][j]++;
      if(data[i][j] == 'I') sumi[i][j]++;
    }
  }
}

int main(){
  cin >> y >> x;
  string trash;
  int num;
  cin >> num;
  getline(cin,trash);
  for(int i = 1; i <= y; i++){
    string str;
    getline(cin,str);
    data[i] = " ";
    data[i] += str;
  }
  make();
  for(int i = 0; i < num; i++){
    int po[4];
    cin >> po[0] >> po[1] >> po[2] >> po[3];
    cout << sumj[po[2]][po[3]]-sumj[po[2]][po[1]-1]-sumj[po[0]-1][po[3]]+sumj[po[0]-1][po[1]-1] << " ";
    cout << sumo[po[2]][po[3]]-sumo[po[2]][po[1]-1]-sumo[po[0]-1][po[3]]+sumo[po[0]-1][po[1]-1] << " ";
    cout << sumi[po[2]][po[3]]-sumi[po[2]][po[1]-1]-sumi[po[0]-1][po[3]]+sumi[po[0]-1][po[1]-1] <<endl;
  }
  return 0;
}