#include <bits/stdc++.h>
using namespace std;
int n, m, sx, sy, tx, ty;
char c[105][105];
struct node
{
int x, y, step;
};
int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
int bfs(int x, int y)
{
queue<node> q;
q.push((node){x, y, 0});
bool vis[105][105] = {};
vis[x][y] = true;
while (!q.empty())
{
node u = q.front();
q.pop();
if (u.x == tx && u.y == ty)
return u.step;
for (int i = 0; i < 4; i++)
{
int xx = u.x + dir[i][0];
int yy = u.y + dir[i][1];
if (xx >= 0 && yy >= 0 && xx < n && yy < m && vis[xx][yy] == false && c[xx][yy] != '#')
{
vis[xx][yy] = true;
q.push((node){xx, yy, u.step + 1});
}
}
}
return -1;
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> c[i][j];
if (c[i][j] == 'S')
{
sx = i, sy = j;
}
if (c[i][j] == 'T')
{
tx = i, ty = j;
}
}
int ans = bfs(sx, sy);
cout << ans;
return 0;
}