#include<bits/stdc++.h>
using namespace std;
int m,n,t,res[2][4]={1,-1,0,0, 0,0,1,-1};
char a[105][105];
bool b[105][105],k;
void dfs(int h,int l,int c)
{
if(a[h][l]=='E')
{
cout<<c<<'\n';
k=true;
return;
}
for(int i=0;i<4;++i)
{
int nh=h+res[0][i];
int nl=l+res[1][i];
if(k==1)
return;
if(1>nh||nh>n||1>nl||nl>m)
continue;
if(a[nh][nl]=='-'&&b[nh][nl]==false)
{
b[nh][nl]=true;
dfs(nh,nl,c+1);
b[nh][nl]=false;
}
}
}
int main()
{
cin>>t;
while(t--)
{
memset(b,false,sizeof(b));
cin>>n>>m;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
cin>>a[i][j];
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(a[i][j]=='S')
{
k=false;
b[i][j]=true;
dfs(i,j,0);
}
}
}
没有结果555……