/*
题意不讲了,大家都看得懂。
这题难点在于走过梯子的时候判断是否要停留一分钟,看代码中的判断方法。
*/
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#define N 22
using namespace std;
int n, m, mark[N][N], dir[4][2] = {0,1, 1,0, 0,-1, -1,0};
int s_x, s_y, t_x, t_y;
char str[N][N];
struct node
{
int x, y, step;
friend bool operator < (node n1, node n2)
{
return n2.step < n1.step;
}
};
int judge(int x, int y)
{
if(x>=0 && x<n && y>=0 && y<m && mark[x][y]!=1 && str[x][y] != '*')
return 1;
else
return 0;
}
int bfs()
{
int i, x, y;
node cur, next;
priority_queue<node>q;
cur.x=s_x; cur.y=s_y; cur.step=0;
q.push(cur);
while(!q.empty())
{
cur = q.top();
q.pop();
if(cur.x==t_x && cur.y==t_y) return cur.step;
int t = 1, tt=1;
for( i=0; i < 4; i++ )
{
next.x = x = cur.x + dir[i][0]*t;
next.y = y = cur.y + dir[i][1]*t;
next.step = cur.step + 1*tt;
t = 1; tt=1;
if(judge(x, y))
{
if(mark[x][y]==2 || mark[x][y]==-2)
{
if((cur.step%2==1 && (i%2==0 && mark[x][y]==2 || i%2!=0 && mark[x][y]==-2)) || (cur.step%2==0 && (i%2==0 && mark[x][y]==-2 || i%2!=0 && mark[x][y]==2)))//判断点
{
t=2; i--;
continue;
}
else
{
t=2; i--; tt=2;
continue;
}
}
mark[x][y] = 1;
q.push(next);
}
}
}
return -1;
}
int main()
{
int i, j;
while(scanf("%d%d", &n, &m) != -1)
{
memset(mark, 0, sizeof(mark));
for( i=0; i < n; i++ )
scanf("%s", str[i]);
for(i=0; i < n; i++ )
for( j=0; j < m; j++ )
{
if(str[i][j] == 'S')
s_x=i, s_y=j;
else if(str[i][j] == 'T')
t_x=i, t_y=j;
else if(str[i][j] == '|')
mark[i][j] = 2;
else if(str[i][j] == '-')
mark[i][j] = -2;
}
mark[s_x][s_y] = 1;
int ans = bfs();
printf("%d\n", ans);
}
return 0;
}