import std.stdio;
import std.string;
import std.conv;

struct Point {
    int x;
    int y;
}

struct Rect {
    Point start;
    Point end;
}

void main() {
    
    // input
    int m, n, k;
    {
        auto temp = readln.split;
        m = to!int(temp[0]);
        n = to!int(temp[1]);
    }
    k = to!int(readln.chomp);
    
    int[][] jungle, ocean, ice;
    jungle.length = m;
    ocean.length = m;
    ice.length = m;
    
    for(int i=0; i<m; ++i) {
        jungle[i].length = n;
        ocean[i].length = n;
        ice[i].length = n;
    }
    
    for(int i=0; i<m; ++i) {
        auto str = readln.chomp;
        for(int j=0; j<n; ++j) {
            final switch(str[j]) {
                case 'J':
                    jungle[i][j] = j == 0 ? 1 : jungle[i][j - 1] + 1;
                    ocean[i][j] = j == 0 ? 0 : ocean[i][j - 1];
                    ice[i][j] = j == 0 ? 0 : ice[i][j - 1];
                    break;
                
                case 'O':
                    jungle[i][j] = j == 0 ? 0 : jungle[i][j - 1];
                    ocean[i][j] = j == 0 ? 1 : ocean[i][j - 1] + 1;
                    ice[i][j] = j == 0 ? 0 : ice[i][j - 1];
                    break;
                
                case 'I':
                    jungle[i][j] = j == 0 ? 0 : jungle[i][j - 1];
                    ocean[i][j] = j == 0 ? 0 : ocean[i][j - 1];
                    ice[i][j] = j == 0 ? 1 : ice[i][j - 1] + 1;

                    break;
            }
        }
    }
    
    Rect[] r;
    r.length = k;
    
    for(int i=0; i<k; ++i) {
        auto temp = readln.split;
        r[i].start.x = to!int(temp[0]) -1;
        r[i].start.y = to!int(temp[1]) -1;
        r[i].end.x = to!int(temp[2]) -1;
        r[i].end.y = to!int(temp[3]) -1;
    }
    
    // calc
    for(int i=0; i<k; ++i) {
        int jungleNum, oceanNum, iceNum;
        
        for(int x=r[i].start.x; x<=r[i].end.x; ++x) {
            int startY = r[i].start.y, endY = r[i].end.y;
            jungleNum += jungle[x][endY] - (startY == 0 ? 0 : jungle[x][startY - 1]);
            oceanNum += ocean[x][endY] - (startY == 0 ? 0 : ocean[x][startY - 1]);
            iceNum += ice[x][endY] - (startY == 0 ? 0 : ice[x][startY - 1]);
        }
        
        // output
        writefln("%d %d %d", jungleNum, oceanNum, iceNum);
    }
    
}