/*************************************************************************
> File Name: code/2015summer/0714/M.cpp
> Author: 111qqz
> Email: rkz2013@126.com
> Created tim: 2015年07月14日 星期二 11时37分51秒
************************************************************************/
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)
typedef long long LL;
typedef unsigned long long ULL;
const int N=1E3+5;
int m,n;
char mymap[N][N];
int tx,ty,sx,sy;
int dirx[4]={0,1,0,-1};
int diry[4]={1,0,-1,0};
int tim[N][N];
bool ok(int xxx,int yyy)
{
if (mymap[xxx][yyy]=='*')
return false;
if (xxx<0||xxx>=m) return false;
if (yyy<0||yyy>=n) return false;
return true;
}
void bfs()
{
cout<<"wwwwwwwwwwwwwwwffffffkkkkkkk?!"<<endl;
memset(tim,0,sizeof(tim));
queue<int>x;
queue<int>y;
x.push(sx);
y.push(sy);
while (!x.empty()&&!y.empty())
{
int xx=x.front();
int yy=y.front();
cout<<"nowx:"<<xx<<" nowy:"<<yy<<endl;
x.pop();
y.pop();
if (xx==tx&&yy==ty) break;
for ( int i = 0 ; i < 4 ; i++ )
{
int tmpx = xx+dirx[i];
int tmpy = yy=diry[i];
if (ok(tmpx,tmpy))
{
if (mymap[tmpx][tmpy]=='|')
{
if (tim[xx][yy]%2==0)
{
if (i==1)
{
tim[tmpx+1][tmpy]=tim[xx][yy]+1;
x.push(tmpx+1);
y.push(tmpy);
}
if (i==3)
{
tim[tmpx-1][tmpy]=tim[xx][yy]+1;
x.push(tmpx-1);
y.push(tmpy);
}
}
else
{
if (i==0)
{
tim[tmpx][tmpy+1]=tim[xx][yy]+1;
x.push(tmpx);
y.push(tmpy+1);
}
....
{
tim[tmpx][tmpy-1]=tim[xx][yy]+1;
x.push(tmpx);
y.push(tmpy-1);
}
}
}
if (mymap[tmpx][tmpy]=='-')
{
if (tim[xx][yy]%2==0)
{
if (i==0)
{
tim[tmpx][tmpy+1]=tim[xx][yy]+1;
x.push(tmpx);
y.push(tmpy+1);
}
if (i==2)
{
tim[tmpx][tmpy-1]=tim[xx][yy]+1;
x.push(tmpx);
y.push(tmpy-1);
}
}
else
{
if (i==1)
{
tim[tmpx+1][tmpy]=tim[xx][yy]+1;
x.push(tmpx+1);
y.push(tmpy);
}
if (i==3)
{
tim[tmpx-1][tmpy]=tim[xx][yy]+1;
x.push(tmpx-1);
y.push(tmpy);
}
}
}
if (mymap[tmpx][tmpy]=='.')
{
tim[tmpx][tmpy]=tim[xx][yy]+1;
x.push(tmpx);
y.push(tmpy);
}
}
}
}
}
void init()
{
scanf("%d %d",&m,&n)
getchar();
for ( int i = 0 ; i < m ; i++ )
for ( int j = 0 ; j < n ; j++)
{
scanf("%c",&mymap[i][j]);
if (mymap[i][j]=='T')
{
tx = i;
ty = j;
}
if (mymap[i][j]=='S')
{
sx = i;
sy = j;
}
}
}
int main()
{
init();
cout<<"why!!!!"<<endl;
bfs();
cout<<tim[tx][ty]<<endl;
return 0;
}