#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxr=25+5;
const int maxc=25+5;
int R,C,sr,sc,tr,tc;
char maze[maxr][maxc];
struct State
{
int r,c,dir,color;
State(int r,int c,int dir,int color):r(r),c(c),dir(dir),color(color){}
}; //构造函数
const int dr[]={-1,0,1,0}; //north,west,south,east
const int dc[]={0,-1,0,1};
int d[maxr][maxc][4][5], vis[maxr][maxc][4][5];
//记录距离(x,y,朝向,颜色) 是否访问
int ans;
queue<State>Q;
void update(int r,int c,int dir,int color,int v)
{
if(r<0||r>=R||c<0||c>=C) return; //不能越界 最后一个bug在这里,第二个r打成c ctm mmp
if(maze[r][c]=='.'&&!vis[r][c][dir][color]){ //可达并且没被访问过
Q.push(State(r,c,dir,color));//进队
vis[r][c][dir][color]=1;
d[r][c][dir][color]=v;
if(r==tr&&c==tc&&color==0) ans=min(ans,v);//更新答案
}
}
void bfs(State st)
{
d[st.r][st.c][st.dir][st.color]=0;
vis[st.r][st.c][st.dir][st.color]=1;
Q.push(st);
while(!Q.empty()){
st=Q.front();Q.pop();
int v=d[st.r][st.c][st.dir][st.color]+1;
update(st.r,st.c,(st.dir+1)%4,st.color,v);//左转
update(st.r,st.c,(st.dir+3)%4,st.color,v);//右转
update(st.r+dr[st.dir],st.c+dc[st.dir],st.dir,(st.color+1)%5,v);//前进
}
}
int main()
{
int kcase=0;
while(scanf("%d%d",&R,&C)==2&&R&&C){
for(int i=0;i<R;i++){
scanf("%s",maze[i]);
for(int j=0;j<C;j++)
if(maze[i][j]=='S'){
sr=i;sc=j;
}
else if(maze[i][j]=='T'){
tr=i;tc=j;//一开始把tr打成tc一直wa,改完之后直接崩掉ORZ
} //终于找到原因,运行的是另一个程序,因为一个工程中只能有一个main
//只要右键 激活当前项目就行了,激活后为粗黑体,但是还有bug OTZ
} //似乎什么都没动,但之前过不了的样例过了,但是还是wa哈哈哈哈
maze[sr][sc]=maze[tr][tc]='.';
ans=INF;
memset(vis,0,sizeof(vis));
bfs(State(sr,sc,0,0));
if(kcase>0) printf("\n");
printf("Case #%d\n",++kcase);
if(ans==INF) printf("destination not reachable\n");
else printf("minimum time = %d sec\n",ans);
}
}