三元组 (x坐标,y坐标,步长)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class T1101献给阿尔吉侬的花束 {
static char[][] map = new char[205][205];
static int[] dx = new int[]{1, -1, 0, 0};
static int[] dy = new int[]{0, 0, 1, -1};
static int r, c, sx, sy, ex, ey;
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(bf.readLine());
String[] temp;
while(t-- > 0){
temp = bf.readLine().split(" ");
r = Integer.parseInt(temp[0]);c = Integer.parseInt(temp[1]);
for (int i = 0; i < r; i++) {
temp = bf.readLine().split("");
for (int j = 0; j < c; j++) {
map[i][j] = temp[j].charAt(0);
if(map[i][j] == 'S'){
sx = i; sy = j;
}else if(map[i][j] == 'E'){
ex = i; ey = j;
}
}
}
Queue<int[]>queue = new LinkedList<>();
int[] cur, nex;
boolean flag = false;
queue.offer(new int[]{sx, sy, 0});
map[sx][sy] = '#';
while(!queue.isEmpty()){
cur = queue.poll();
if(cur[0] == ex && cur[1] == ey){
System.out.println(cur[2]);
flag = true;
break;
}
for (int i = 0; i < 4; i++) {
if(cur[0]+dx[i]>=0 && cur[0]+dx[i]<r && cur[1]+dy[i]>=0 && cur[1]+dy[i]<c && map[cur[0]+dx[i]][cur[1]+dy[i]] != '#'){
nex = new int[]{cur[0]+dx[i], cur[1]+dy[i], cur[2]+1};
map[cur[0]+dx[i]][cur[1]+dy[i]] = '#';
queue.offer(nex);
}
}
}
if(!flag)System.out.println("oop!");
}
}
}