//14
#include<iostream>

using namespace std;

int main(){
  int m,n,k;
  cin>>m>>n>>k;
  typedef int G[1001][1001];
  static G i,o,j;
  for(int y=0;y<m;y++){
    for(int x=0;x<n;x++){
      char c;
      cin>>c;
      i[y+1][x+1]=i[y][x+1]+i[y+1][x]-i[y][x]+(c=='I');
      o[y+1][x+1]=o[y][x+1]+o[y+1][x]-o[y][x]+(c=='O');
      j[y+1][x+1]=j[y][x+1]+j[y+1][x]-j[y][x]+(c=='J');
    }
  }
  while(k--){
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    for(int k=0;k<3;k++){
      int (*p[])[1001]={j,o,i};
      cout<<p[k][c][d]-p[k][a-1][d]-p[k][c][b-1]+p[k][a-1][b-1]<<((k==2)?'\n':' ');
    }
  }
  return 0;
}