#include<bits/stdc++.h>
using namespace std;
char arr[209][209];
int n,m;
int val[209][209][2];
int dp(int i,int j,int p){
//cout<<i<<" "<<j<<" "<<p<<endl;
if(i>=n && j>=m){return 0;}
if(val[i][j][p] != -1){return val[i][j][p];}
int sum=0;
if(p==0){
if(j>0){
if(arr[i][j-1]!='#'){
if(arr[i][j-1]=='T'){sum = max(sum,dp(i,j-1,0)+1);}
else{sum = max(sum,dp(i,j-1,0));}
}
}
if(i<n-1){
if(arr[i+1][j]!='#'){
if(arr[i+1][j]=='T'){sum = max(sum,dp(i+1,j,1)+1);}
else{sum = max(sum,dp(i+1,j,1));}
}
}
}
if(p==1){
if(j<m-1){
if(arr[i][j+1]!='#'){
if(arr[i][j+1]=='T'){sum = max(sum,dp(i,j+1,1)+1);}
else{sum = max(sum,dp(i,j+1,1));}
}
}
if(i<n-1){
if(arr[i+1][j]!='#'){
if(arr[i+1][j]=='T'){sum = max(sum,dp(i+1,j,0)+1);}
else{sum = max(sum,dp(i+1,j,0));}
}
}
}
val[i][j][p] = sum;
return val[i][j][p];
}
int main(){
#ifndef ONLINE_JUDGE
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
#endif
int t;
cin>>t;
while(t--){
cin>>n>>m;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
cin>>arr[i][j];
}
}
int ans=0;
memset(val,-1,sizeof(val));
if(arr[0][0]!='#'){
ans = dp(0,0,1);
if(arr[0][0]=='T'){ans++;}
}
cout<<ans<<endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIApjaGFyIGFyclsyMDldWzIwOV07CmludCBuLG07CmludCB2YWxbMjA5XVsyMDldWzJdOwogCmludCBkcChpbnQgaSxpbnQgaixpbnQgcCl7CgkvL2NvdXQ8PGk8PCIgIjw8ajw8IiAiPDxwPDxlbmRsOwoJaWYoaT49biAmJiBqPj1tKXtyZXR1cm4gMDt9CglpZih2YWxbaV1bal1bcF0g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