Now SuSu and his companions arrived at Xiangling's hometown - ZiRong Forest. To drive SuSu out of these area, "Piaorou Bro" Lingduan set the forest on fire. To prevent the trees from burning down, SuSu must find the exit of the forest as quickly as possible and kill Lingduan. If he could not kill Lingduan in TIME seconds, the forest will suffer too much damage to be recovered. What was worse, there are some horrible monsters in the forest. SuSu would suffer a certain damage when he kill a monster on his way to the exit. For the purpose of save energy and to kill Lingduan more easily, the damage SuSu suffer from the monsters should be the fewer the better. Please help SuSu to find a reasonable path which can lead him to the exit in TIME seconds, and suffers the minimum damage at the same time.
To represents the ZiRong forest, we use a n * m maze:
'S' and 'L' represents the position of SuSu and Lingduan at the very beginning. Each of them appears in the maze once and only once.
'F' represents fire where SuSu can not step into.
A number (from 0 to 9) indicates there is one monster in this grid ,and it's attack is the corresponding number.
Every second SuSu can choose a direction and go forward 1 ~ K steps, and for all monsters' attack in the grids he passed by(if the grid do exist a monster), we accumulate them and get a sum. The damage SuSu will suffer is the square of this sum.