//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cmath>
#include<climits>
#include<string>
#include<map>
#include<queue>
#include<vector>
#include<stack>
#include<set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
#define pb(a) push(a)
#define INF 0x1f1f1f1f
#define lson idx<<1,l,mid
#define rson idx<<2|1,mid+1,r
#define PI 3.1415926535898
template<class T> T min(const T& a,const T& b,const T& c) {
return min(min(a,b),min(a,c));
}
template<class T> T max(const T& a,const T& b,const T& c) {
return max(max(a,b),max(a,c));
}
void debug() {
#ifdef ONLINE_JUDGE
#else
freopen("d:\\in1.txt","r",stdin);
freopen("d:\\out1.txt","w",stdout);
#endif
}
int getch() {
int ch;
while((ch=getchar())!=EOF) {
if(ch!=' '&&ch!='\n')return ch;
}
return EOF;
}
const int maxn=1000100;
char grid[22][22];
int vis[22][22];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
int n,m;
struct point
{
int x,y,t;
bool operator < (const point &pt) const
{
return t>pt.t;
}
};
int bfs()
{
priority_queue<point> q;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(grid[i][j]=='S')
{
q.push((point){i,j,0});
vis[i][j]=1;
break;
}
while(!q.empty())
{
point pt=q.top();q.pop();
if(grid[pt.x][pt.y]=='T')return pt.t;
for(int d=0;d<4;d++)
{
int nx=pt.x+dx[d];
int ny=pt.y+dy[d];
int nt=pt.t+1;
if(nx>=1&&nx<=n&&ny>=1&&ny<=m)
{
if((grid[nx][ny]=='.'||grid[nx][ny]=='T')&&vis[nx][ny]!=1)
{
q.push( (point) {nx,ny,nt} );
vis[nx][ny]=1;
}
else if(grid[nx][ny]=='|')
{
if(nt%2==d%2)nt++;
int nnx=nx+dx[d],nny=ny+dy[d];
if(nnx>=1&&nnx<=n&&nny>=1&&nny<=m&&!vis[nnx][nny])
{
q.push( (point) {nnx,nny,nt} );
vis[nnx][nny]=1;
}
}
else if(grid[nx][ny]=='-')
{
if(nt%2!=d%2)nt++;
int nnx=nx+dx[d],nny=ny+dy[d];
if(nnx>=1&&nnx<=n&&nny>=1&&nny<=m&&!vis[nnx][nny])
{
q.push( (point) {nnx,nny,nt} );
vis[nnx][nny]=1;
}
}
}
}
}
return -1;
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
scanf("%s",grid[i]+1);
int num=bfs();
printf("%d\n",num);
}
return 0;
}