char g[333][333];
int n,m;
struct node{
int x,y;
int step;
friend bool operator <(node a,node b){
return a.step>b.step;
}
};
node st,end;
bool vis[333][333];
int d[4][2] = {0,1,0,-1,1,0,-1,0};
int bfs(){
int i,j,k;
priority_queue<node> pp;
st.step = 0;
vis[st.x][st.y] = 1;
pp.push(st);
node cur,next;
while(!pp.empty()){
cur = pp.top();
pp.pop();
if(g[cur.x][cur.y]=='T'){
return cur.step;
}
for(i=0;i<4;i++){
int x = cur.x+d[i][0];
int y = cur.y+d[i][1];
if(x<0 || y<0 || x>=n || y>=m || vis[x][y])continue;
vis[x][y] = 1;
if(g[x][y]=='B'){
next.x = x;
next.y = y;
next.step = cur.step +2;
pp.push(next);
}
if(g[x][y]=='E'){
next.x = x;
next.y = y;
next.step = cur.step+1;
pp.push(next);
}
if(g[x][y]=='T'){
next.x = x;
next.y = y;
next.step = cur.step+1;
pp.push(next);
}
}
}
return -1;
}
int main(){
while(scanf("%d%d",&n,&m) && (n+m)){
int i,j;
for(i=0;i<n;i++){
scanf("%s",g[i]);
for(j=0;j<m;j++){
if(g[i][j]=='Y'){
st.x = i,st.y = j;
}
if(g[i][j]=='T'){
end.x = i,end.y = j;
}
vis[i][j] = 0;
}
}
int ans = bfs();
printf("%d\n",ans);
}
return 0;
}