#include <cstdio>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int joi[3][1000][1000];

int main(){
  const char *s = "JOI";
  const int h = getInt();
  const int w = getInt();
  const int m = getInt();

  REP(i,h){
    char g[1010];
    scanf("%s", g);

    REP(j,w){
      REP(k,3) if(g[j] == s[k])
        joi[k][i][j]++;
    }
  }

  REP(k,3) REP(i,h) REP(j,w){
    if(i != 0) joi[k][i][j] += joi[k][i - 1][j];
    if(j != 0) joi[k][i][j] += joi[k][i][j - 1];
    if(i != 0 && j != 0)
      joi[k][i][j] -= joi[k][i - 1][j - 1];
  }

  REP(i,m){
    const int y1 = getInt() - 1;
    const int x1 = getInt() - 1;
    const int y2 = getInt() - 1;
    const int x2 = getInt() - 1;

    REP(k,3){
      int ans = joi[k][y2][x2];
      if(x1 != 0) ans -= joi[k][y2][x1 - 1];
      if(y1 != 0) ans -= joi[k][y1 - 1][x2];
      if(x1 != 0 && y1 != 0)
        ans += joi[k][y1 - 1][x1 - 1];
      printf("%d%c", ans, k == 2 ? '\n' : ' ');
    }
  }

  return 0;
}