#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#define x first
#define y second
using namespace std;
const int N=210;
typedef pair<int,int> PII;
int n,m;
char g[N][N];
int d[N][N];
int bfs(PII start,PII end)
{
queue<PII> q;
memset(d,-1,sizeof d);
d[start.x][end.y]=0;
q.push(start);
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
while(q.size())
{
PII t=front();
q.pop();
for(int i=0;i<n;i++)
{
int a=t.x=dx[i],b=t.y+dy[i];
if(a<0||a>=n||b<0||b>=m) continue;
if(d[a][b]!=-1) continue;
if(g[a][b]=='#') continue;
d[a][b]=d[t.x][t.y]+1;
if(end==make_pari(a,b)) return d[a][b];
q.push(a,b);
}
}
return -1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
PII start,end;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
if(g[i][j]=='S') start={i,j};
else if(g[i][j]=='E') end={i,j};
int distance=bfs(start,end);
if(distance==-1) printf("opp!\n");
else printf("%d\n",distance);
return 0;
}
}