#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <functional>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int w, h, k, ssum[3][1111][1111];
char mas[1111][1111];
map <int, int> enc;


int main(){
  
  enc['J'] = 0, enc['O'] = 1, enc['I'] = 2;
  enc[0] = 'J', enc[1] = 'O', enc[2] = 'I';

  cin >> h >> w >> k;
  for(int i = 0; i < h; i++)
    for(int j = 0; j < w; j++){ 
      cin >> mas[i][j];
      ssum[enc[mas[i][j]]][i+1][j+1] = 1;
    }           
  
  for(int l = 0; l < 3; l++){

    for(int i = 0; i < h; i++){
      for(int j = 1; j < w; j++){
        ssum[l][i+1][j+1] += ssum[l][i+1][j];
      }
    }

    for(int i = 1; i < h; i++){
      for(int j = 0; j < w; j++){
        ssum[l][i+1][j+1] += ssum[l][i][j+1];
      }
    }
  }
   
  while(k--){
    int a, b, c, d; 
    cin >> a >> b >> c >> d;
    for(int l = 0; l < 3; l++){
      cout << ssum[l][c][d] + ssum[l][a-1][b-1] - ssum[l][c][b-1] - ssum[l][a-1][d] << (l != 2 ? ' ' : '\n');
    }
  }
                                           

}