//求迷宫问题中的最短路径
#include<iostream>
using namespace std;
int n, m;
int dir[2][4] = { { 1,0,-1,0 } ,{0,-1,0,1} };
char maze[1005][1005];
bool vis[1005][1005];
int min_step=10086;
int cnt;
int x, y, tx, ty;
void dfs(int x, int y, int cnt) {
if ( maze[x][y] =='T'&& cnt <min_step) {
min_step = cnt;
cout << "min_step:" << min_step << endl;
return;
}
if (x >= 0 && x < n && y >= 0 && y < m && maze[x][y] != '#' &&vis[x][y]==false )
{
vis[x][y] = true;
for (int i = 0; i < 4; i++) {
tx = x + dir[0][i];
ty = y+ dir[1][i];
dfs(tx, ty, cnt + 1);
}
vis[x][y] = false;
}
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++) {
for ( int j = 0; j < m; j++) {
cin >> maze[i][j];
vis[i][j] = false;
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (maze[i][j] == 'S') {
x = i;
y = j;
dfs(x, y, 0);
}
}
}
return 0;
}