package Toj2470;
import java.util.Scanner;
class Robot {
public int x;
public int y;
public int step;
public int faceDire;
public Robot(int x, int y, int step, int faceDire){
this.x = x;
this.y = y;
this.step = step;
this.faceDire = faceDire;
}
}
class Queue {
public Robot[] myRobot = new Robot[100000];
public int front = 0;
public int rear = 0;
public void push(Robot robot) {
myRobot[rear++] = robot;
}
public Robot pop() {
return myRobot[front++];
}
public boolean isEmpty() {
return front == rear;
}
}
// 0 up
// 1 right
// 2 down
// 3 left
public class Tojtwo {
public static int turnOver[] = { 0, 1, 2, -1 };
public static int m;
public static int n;
public static int mazeMap[][];
public static int startx;
public static int starty;
public static int endx;
public static int endy;
public static int dirx[]={-1,0,1,0};
public static int diry[]={0,1,0,-1};
public static void bFS() {
Robot myRobot = new Robot(startx,starty,0,2);
Queue myQueue=new Queue();
myQueue.push(myRobot);
while(!myQueue.isEmpty()){
Robot curRobot=myQueue.pop();
int curx=curRobot.x;
int cury=curRobot.y;
int curStep=curRobot.step;
int curFaceDire=curRobot.faceDire;
int nextx;
int nexty;
for(int i=0;i<4;i++){
nextx=curx+dirx[i];
nexty=cury+diry[i];
if(nextx>=0&&nextx<m&&nexty>=0&&nexty<n&&mazeMap[nextx][nexty]==0){
}
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int casenum = sc.nextInt();
for (int k = 0; k < casenum; k++) {
///
m = sc.nextInt();
n = sc.nextInt();
char maze[][] = new char[m][n];
mazeMap= new int[m][n];
String[] str = new String[m];
for (int i = 0; i < m; i++) {
str[i] = sc.next();
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
maze[i][j] = str[i].charAt(j);
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (maze[i][j] == '#')
mazeMap[i][j] = 1;
if (maze[i][j] == '.')
mazeMap[i][j] = 0;
if (maze[i][j] == 'S') {
startx = i;
starty = j;
mazeMap[i][j] = 0;
}
if (maze[i][j] == 'T') {
endx = i;
endy = j;
mazeMap[i][j] = 0;
}
}
}
///
}
}
}