up
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
#define x first
#define y second
const int N=205;
int T,r,c;
char g[N][N];
int dist[N][N];
PII eend;
int bfs(PII start){
queue<PII>q;
memset(dist,-1,sizeof(dist));
dist[start.x][start.y]=0;
q.push(start);
int dx[]={0,-1,0,1},dy[]={1,0,-1,0};
while(q.size()){
PII t=q.front();
q.pop();
for(int i=0;i<4;i++){
int a=t.x+dx[i],b=t.y+dy[i];
if(a<0||a>=r||b<0||b>=c)continue;
if(dist[a][b]!=-1)continue;
if(g[a][b]=='#')continue;
dist[a][b]=dist[t.x][t.y]+1;
if(eend==(PII){a,b})return dist[a][b];
q.push({a,b});
}
}
return -1;
}
int main(){
cin>>T;
PII start;
while(T--){
cin>>r>>c;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
cin>>g[i][j];
if(g[i][j]=='S')start={i,j};
if(g[i][j]=='E')eend={i,j};
}
}
int ans=bfs(start);
if(ans==-1)cout<<"oop!"<<endl;
else cout<<ans<<endl;
}
}