Tatsumi and Mine have decided to go out and buy a nice fir tree to celebrate their first holiday season together. They've headed to a nearby forest, and are now observing a gathering of trees
However, the trees are packed so tight that to get to the tallest tree they may have to blast cut their way through a number of trees. Being conscious of the environment, they wish to minimize the total heights of the trees they clear. Knowing that they can only walk in cardinal directions, how many trees must they remove to arrive at their desired tree? The sum of the heights of the trees you cut must be the minimal over all possible sets of trees to cut to get to the tallest tree. Additionally, you should also minimize the number of trees summing to that minimal height in the event of a tie.
Scoring
For
Input Specification
The first line will contain the integers
The next
A .
denotes that there is no tree at that position, and the couple start off at the position marked with an X
.
Output Specification
One integer, the number of trees Tatsumi and Mine will have to remove to get to their desired tree.
Sample Input 1
5 5
3 . . 2 3
. . 2 2 2
. 2 2 2 2
2 2 2 2 2
. . . . X
Sample Output 1
2
Explanation of Output for Sample Input 1
As in
Sample Input 2
4 8
1 1 1 1 1 1 1 9
. . 1 1 5 1 3 4
. . . . . . . .
X . . 8 7 6 . .
Sample Output 2
3
Explanation of Output for Sample Input 2
The tallest tree is that where
Comments
In Sample Output 1 we are minimizing the number of trees cut; in Sample Output 2 we are minimizing the heights of the trees cut.
We are trying to minimize the sum of heights, but we do not actually have to output this minimal sum (it is a bit strange, I know).
Instead, after we have the minimal height, call it
, we want to find the minimum number of trees on a path from the starting point to the target tree that sum to the minimum 
.