This page is still under construction.

Parts of this page are still being built. What you see may change.

Racing

Interview

Time limit2sMemory limit1024 MB

Summary
A car slides from its cell toward a chosen wall, stops half the remaining distance from the first obstacle, and we need the fewest presses to reach the target.
Level

Medium6 of 10

Topics
BFS, Graph, Simulation, Implementation
Solved
No attempts yet

Problem

For his birthday, young technician Misha received a radio-controlled car. Misha quickly grew bored of driving the car back and forth around the room, so he built a special track. He divided the room into square cells, left some of them empty, and placed obstacles in others. For a whole week, Misha improved his record for completing the track every day. But how disappointed he was when his friend Tima came to visit with his own car and beat his record. It became clear that the car had to be upgraded.

During test runs made a day later, Misha discovered that the car did indeed drive better, but its behavior had changed somewhat. Now only four buttons on the remote work: forward, backward, right, left. When one of them is pressed, the car drives toward the corresponding wall of the room, which is also the boundary of the track, exactly perpendicular to that wall. The car accelerates so much that it stops responding to other commands, crashes into the nearest obstacle or wall, and bounces off it by half the distance it traveled. That is, if there were xx empty cells between the car and the wall, then after the bounce it stops on the cell that is ⌊x2⌋\left\lfloor\frac{x}{2}\right\rfloor cells from the wall (⌊x⌋\lfloor x\rfloor denotes rounding down; for example, ⌊42⌋=2\left\lfloor\frac{4}{2}\right\rfloor=2, ⌊52⌋=2\left\lfloor\frac{5}{2}\right\rfloor=2).

Now Misha wonders what the minimum number of times the remote buttons must be pressed for the car, starting in the start cell, to stop in the finish cell.

Input

The first line of the input file contains two integers nn and mm, the dimensions of the track (2≤m,n≤202 \le m, n \le 20). The next nn lines contain mm characters each: the character <<.>> corresponds to an empty cell, <<\#>> to an obstacle, and <<S>> and <<T>> to the start cell and the finish cell, respectively.

Output

Print the minimum number of button presses on the remote needed to drive the car along the track from the start to the finish.

If it is impossible to get from the start to the finish, print −1-1.

Examples1

  1. Example 1

    Input
    5 5
    S#..T
    .#.##
    .....
    .##.#
    .#...
    
    Expected output
    6