#include<bits/stdc++.h>
using namespace std;
struct Node{
int x, y, d, c;
Node(int x = 0, int y = 0, int d = 0, int c = 0):x(x), y(y), d(d), c(c){}
bool operator==(const Node& node)const{
return node.x == x && node.y == y && node.c == c; //direction is not needed.
}
};
const int maxn = 25 + 3;
int R, C;
char maze[maxn][maxn];
int vis[maxn][maxn][4][5];
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
int solve()
{
Node S, T;
for(int i = 0; i < R; i++){
for(int j = 0; j < C; j++){
char ch = maze[i][j];
if(ch == '.' || ch == '#') continue;
if(ch == 'S') S = {i, j, 0, 0};
else T = {i, j, 0, 0};
}
}
memset(vis, -1, sizeof(vis));
vis[S.x][S.y][S.d][S.c] = 0;
queue<Node>q;
q.push(S);
while(!q.empty()){
Node u = q.front(); q.pop();
int x = u.x, y = u.y, d = u.d, c = u.c;
if(T == u) return vis[x][y][d][c];
int v = vis[x][y][d][c] + 1;
//go head
int xx = x + dx[d], yy = y + dy[d];
if(xx >= 0 && xx < R && yy >= 0 && yy < C && maze[xx][yy] != '#' && vis[xx][yy][d][c] == -1){
vis[xx][yy][d][(c+1)%5] = v;
q.push(Node(xx, yy, d, (c+1)%5));
}
//turn, clockwise
if(vis[x][y][(d+1)%4][c] == -1) {q.push(Node(x, y, (d+1)%4, c)); vis[x][y][(d+1)%4][c] = v;}
if(vis[x][y][(d+3)%4][c] == -1) {q.push(Node(x, y, (d+3)%4, c)); vis[x][y][(d+3)%4][c] = v;}
}
return -1;
}
int main()
{
int cnt = 0;
while(cin >> R >> C && R){
for(int i = 0; i < R; i++){
scanf("%s", maze[i]);
}
if(++cnt != 1) printf("\n");
int ans = solve();
if(ans == -1) printf("Case #%d\ndestination not reachable\n", cnt);
else printf("Case #%d\nminimum time = %d sec\n", cnt, ans);
}
return 0;
}
/*
3 1
T
.
S
*/