Capybara-san really likes meat (vegan-friendly), so now he wants to gather a lot of meats and saves all of them in his tent.
He start from his tent (letter 'S') and want to gather all meat in the maze (letter 'M')
He can only bring one meat per trip and he wants to take all the meat to his tent.
If Capybara-san is at position (x, y), then he can walk to:
All 4 movements are counted by 1 step, and also valid if that position is not a wall
What is the smallest step does Capybara-san need to take to gather all the meat back to his tent?
The maze is made as:
Sample IO Explanation:
The first input is X, and Y (X is row, Y is column) of the map
The next input is X * Y map,
5 <= X, Y <= 500
It's guaranteed that there is always S, M, and a path between that 2 points
Output the steps, following with a newline