Warning: This statement was translated using an AI. Results might not be fully accurate or faithful to the original statement. Please report any potential errors on our Discord.
Task
Casi Rumba is a very serious teacher. When the CS class starts, he sits at the teacher's desk and gives the kids work. In the classroom, everyone is focused on solving problems, except for Sebastian. Every time Casi is not looking at him, he puts his headphones on in second and starts listening to music. Casi, being very strict, does not accept such behavior in his class. When he sees Sebi, he stands up and hastily heads toward the student to scold him, moving one cell per second. Sebi can take his headphones off instantly at any moment, thus avoiding the punishment he was about to receive ( push-ups). Once the kid becomes attentive again, the teacher will move back to the desk in a relaxed manner, at a speed of one cell every seconds.
The student does not want to be caught inattentive, so given the classroom layout, including the positions of the desks, marked with , the position of the teacher’s desk, marked with , the position of Sebastian, marked with , and the fact that the class lasts seconds, compute the maximum time the student can spend listening to music without being caught by Casi.
Input data
The first line of the input file casirumba.in
contains three integers, , , and , representing the length and width of the classroom, as well as how many seconds the class lasts.
The next lines contain characters, representing the layout of the classroom in which the class is held.
Output data
The first line of the output file casirumba.out
should contain a single integer, the maximum number of seconds during which Sebastian listens to music.
Constraints and clarifications
- The desk from which Casi starts is marked with , the desks of the other students are marked with , and Sebi’s desk is marked with . Empty cells (which contain nothing and are passable) are marked with 0.
- The characters and appear exactly once.
- It is guaranteed that there exists at least one valid path from Casi’s desk to Sebastian.
Example 1
casirumba.in
3 4 10
0 S B B
B 0 0 0
B 0 0 C
casirumba.out
5
Explanation
It takes Casi seconds to get next to Sebi, during which the student listens to music. When the teacher arrives, he takes his headphones off, making the teacher turn back to the desk. After another seconds (the time it takes Casi to move one cell back), the student can listen for more second. This cycle repeats, with Sebi listening for a total of seconds during the class that lasts seconds.
Example 2
casirumba.in
4 5 17
B B 0 0 0
0 0 0 B 0
0 B B B 0
0 0 S B C
casirumba.out
13
Explanation
It takes Casi seconds to get next to Sebi. The student can then listen for more seconds until the end of the class.