Under construction

Still being built. Things may change

Planet Exploration

Interview

Time limit1sMemory limit256 MB

Summary
Given a grid of jungle, sea, and ice cells, answer many rectangle queries by counting how many cells of each terrain type fall inside.
Level

Medium4 of 10

Topics
Prefix sum, Array, Implementation, Matrix
Solved
No attempts yet

Problem

An expedition traveling by spaceship searched for a habitable planet and finally found one. The planet is covered entirely by jungle, sea, and ice. The team drew a map of the habitable region and sent it back to Earth.

The map is a rectangle NN cm wide and MM cm tall, divided into square cells 1 cm on each side. The terrain of each cell is written as a letter: jungle is J, sea is O, and ice is I.

Back on Earth, KK survey regions were chosen. For each region, write a program that counts how many cells of jungle, sea, and ice it contains.

Input

The first line contains the map size MM and NN. (1≤M,N≤10001 \le M, N \le 1000)

The second line contains the number of survey regions KK. (1≤K≤1000001 \le K \le 100000)

Each of the next MM lines contains the map: a string of length NN made up of J, O, and I.

Each of the following KK lines describes one survey region as four integers aa, bb, cc, dd. The region is a rectangle whose top-left cell is (a,b)(a, b) and whose bottom-right cell is (c,d)(c, d), where the first value is the row counted from the top and the second value is the column counted from the left. (1≤a≤c≤M1 \le a \le c \le M, 1≤b≤d≤N1 \le b \le d \le N)

Output

For each survey region, print the number of jungle, sea, and ice cells inside it, separated by spaces, one region per line. In other words, print the counts of J, O, and I in that order.

Hint

The figure below illustrates counting terrain inside one survey region. This region contains 3 jungle (J) cells, 5 sea (O) cells, and 2 ice (I) cells.

Examples1

  1. Example 1

    Input
    4 7
    4
    JIOJOIJ
    IOJOIJO
    JOIJOOI
    OOJJIJO
    3 5 4 7
    2 2 3 6
    2 2 2 2
    1 1 4 7
    
    Expected output
    1 3 2
    3 5 2
    0 1 0
    10 11 7