#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
const int N = 210;
int t, r, c, p = 1;
char g[N][N];
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
bool flge[N][N];
struct shu {
int x, y, step;
};
int Bfs(int x, int y)
{
queue<shu> q;
q.push({x, y, 0});
while(q.size()) {
shu temp = q.front();
q.pop();
int x = temp.x, y = temp.y;
flge[x][y] = true;
for(int i = 0; i < 4; i++){
int a = x + dir[i][0], b = y + dir[i][1];
//出界或者为墙或者已经走过了就继续循环
if(a < 0 || a >= r || b < 0 || b >= c || g[a][b] == '#' || flge[a][b])
continue;
q.push({a, b, temp.step + 1});
//标记一下
flge[a][b] = true;
//如果为奶酪的话就返回
if(g[a][b] == 'E')
return temp.step + 1;
}
}
return -1;
}
int main()
{
cin >> t;
while(p <= t) {
memset(flge, false, sizeof(flge));
cin >> r >> c;
for(int i = 0; i < r; i++)
scanf("%s", g[i]);
int x = 0, y = 0;
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
//获取当前矩阵的S位置
if(g[i][j] == 'S'){
x = i;
y = j;
break;
}
int res = Bfs(x, y);
cout << (res == -1 ? "oop!" : to_string(res)) << endl;
p++;
}
return 0;
}