labirint (COCI CONTEST_#1)
Time limit: 1 seconds | Memory limit: 512 MB | Task type: batch
Algorithms: shortest path, bfs, graph theory, combinatorics

# Task Labirint

What is an EJOI for you? Game room!

Teo is searching for the Croatian EJOI team! She has already found Gabriel, but is still looking for Vito, Dino, and Ivo.

Teo and the EJOI team are in a labyrinth consisting of *n* × *m* rooms, all of the same size. The rooms form a grid. The top-left room is labeled with (1, 1), and the bottom-right with (*n*, *m*). Between each pair of adjacent rooms, there is a door
colored in one of four colors: blue (marked with 'P'), red (marked with 'C'), green (marked with 'Z') and orange (marked with 'N').

![Illustration of the third example. The black circle marks the room in which Teo and Gabriel are located at in the fourth question, and the white circle marks the room in which Vito, Ivo and Dino are located at. The gray path is one of the possible paths that passes through three different door colors.](_page_0_Figure_8.jpeg)

*Illustration of the third example. The black circle marks the room in which Teo and Gabriel are located at in the fourth question, and the white circle marks the room in which Vito, Ivo and Dino are located at. The gray path is one of the possible paths that passes through three different door colors.*

At some point, Gabriel says: *I know where the rest of the team is, but I will only tell you if you can answer all of my questions.*

Gabriel's questions are: *If we are currently in room (a<sub>i</sub>, b<sub>i</sub>) and the rest of the team is in room (c<sub>i</sub>, d<sub>i</sub>), what is the minimum number of door colors we have to go through to reach them?*

Teo is very good at answering Gabriel's questions, but there are simply too many of them. She does not have much time (the bus is leaving soon), so she is asking you to help her answer *q* of Gabriel's questions!

## Input

The first line contains integers *n* and *m* (1 ≤ *n*, *m* ≤ 100, 1 < *n* × *m*), the number of rooms.

The *i*-th of the following *n* lines contains *m* - 1 characters ('P', 'C', 'Z' or 'N'), where the *j*-th character marks the colour of the door that connects rooms (*i*, *j*) and (*i*, *j* + 1).

The *i*-th of the following *n* - 1 lines contains *m* characters ('P', 'C', 'Z' or 'N'), where the *j*-th character marks the colour of the door that connects rooms (*i*, *j*) and (*i* + 1, *j*).

The next line contains the integer *q* (1 ≤ *q* ≤ 100), the number of Gabriel's questions.

In the *i*-th of the following *q* lines, there are four integers *a<sub>i</sub>*, *b<sub>i</sub>*, *c<sub>i</sub>*, *d<sub>i</sub>* (1 ≤ *a<sub>i</sub>*, *c<sub>i</sub>* ≤ *n*, 1 ≤ *b<sub>i</sub>*, *d<sub>i</sub>* ≤ *m*, (*a<sub>i</sub>*, *b<sub>i</sub>*) ≠ (*c<sub>i</sub>*, *d<sub>i</sub>*)), the description of Gabriel's *i*-th question.

## Output

In the *i*-th of *q* lines, output the answer to Gabriel's *i*-th question.

## Scoring

| Subtask | Points | Constraints                                                                                                                       |
|---------|--------|-----------------------------------------------------------------------------------------------------------------------------------|
| 1       | 11     | *n* = 1                                                                                                                             |
| 2       | 13     | All doors that connect rooms (*i*, *j*) with (*i*, *j* + 1) are blue, and all doors that connect rooms (*i*, *j*) with (*i* + 1, *j*) are red. |
| 3       | 24     | Every door is either red or blue.                                                                                                 |
| 4       | 22     | No additional constraints.                                                                                                        |

#### Examples

| Input   | Input   | Input   |
|---------|---------|---------|
| 1 8     | 3 3     | 4 4     |
| CPZNCCP | PP      | CCC     |
| 4       | PP      | CPC     |
| 1 1 1 8 | PP      | PPP     |
| 1 3 1 5 | CCC     | CNP     |
| 1 8 1 4 | CCC     | ZZZZ    |
| 1 2 1 3 | 3       | PPPP    |
|         | 1 1 3 3 | CPZC    |
|         | 3 3 2 2 | 4       |
| Output  | 1 1 1 3 | 3 1 2 3 |
| 4       |         | 1 1 4 4 |
| 2       | Output  | 2 2 3 3 |
| 3       | 2       | 1 4 4 1 |
| 1       | 2       |         |
|         | 1       | Output  |
|         |         | 1       |
|         |         | 2       |
|         |         | 1       |
|         |         | 3       |

#### Clarification of the third example:

The third example is illustrated in the text.

For the first question, Teo and Gabriel can use just the blue doors to reach the rest of the team; for the second question, they can use blue and green doors; for the third again only blue is enough; and for the fourth, they can use blue, green, and red doors.

Starter code:

Grader code:

Setup script:

Evaluation script: