#00018A

Zidine

While Tomislav was sightseeing Dubrovnik's walls he saw a secret passage.
By going through it, he found himself in a rectangle like room. On the wall he saw a floor plan, and when he go close to it , he heard that doors which he entered are closing.

Soon he saw a poorly visible inscription : Traveler!
You are in the room of keys. To unlock the secret doors and went out of the room, you have to collect determined number of keys. All the keys are in this room and you can get to all of them. On the floor plan positions of keys are marked with a letter 'K'.
Your position has been marked too - look on floor plan for a letter 'X'. All the walls are marked with sign '#'. Empty spots on floor plan are marked with '.'. Secret doors are the only way out of the room. Good Luck!

Tomislav needs to get to competition fast so he want's to know what's the quickest way to collect exactly K keys.

Floor plan of the room is made of N rows, every with M columns.
Tomislav is moving through the room in four main directions - up ,down, left ,right.
Floor plan will always have walls on edges, ie. it will never be possible to leave the room(except using the secret doors that are not marked on floor plan).
Room can have partition (walls) inside. In the room there will be at most 8 keys.


Input
- natural number N (1 ≤ N ≤ 30), number of rows of the floor plan;
- natural number M (1 ≤ M ≤ 30), number of columns of the floor plan;
- natural number K (1 ≤ K ≤ 8), number of keys he needs to collect;
- N rows, in every M signs, floor plan.

Output-natural number S, the least number of steps Tomislav needs to make to collect K keys.


Input :
6
10
2
##########
#.X.....K#
#.....K..#
#........#
#K.......#
##########

Output:
8

Explanation :

Tomislav can collect 2 keys if he first goes
4 fields right, then 1 field down,
then 2 fields right, then 1 field up. If he would first
went to his nearest ( bottom down ) key,
then his total trip would be greater
because he would make four steps to it,
and rest of the keys are remote 7 and 10 steps.



Input:
6
10
2
##########
#.X#....K#
##.#.K#..#
#..####..#
#K.......#
##########

Output:
14

Explanation:

In some test cases walls can be inside room.
Now it's quickest for him to collect
left key (4 steps), then upper right
(10 steps), because the distance to
middle key is now greater.


Submit solution

Coming later

The grading service will be connected in a later migration step. You can inspect the task and your previous results now.