#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 350010
using namespace std;
char s[1010];
int t,r,c,tot;
int vis[MAXN],dis[MAXN];
int to[MAXN*2],net[MAXN*2],cap[MAXN*2],head[MAXN*2];
void add(int u,int v,int w){
to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
to[++tot]=u;net[tot]=head[v];cap[tot]=w;head[v]=tot;
}
void spfa(int s){
queue<int>que;
memset(vis,0,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
que.push(s);
dis[s]=0;vis[s]=1;
while(!que.empty()){
int now=que.front();
que.pop();
vis[now]=0;
for(int i=head[now];i;i=net[i])
if(dis[to[i]]>dis[now]+cap[i]){
dis[to[i]]=dis[now]+cap[i];
if(!vis[to[i]]){
vis[to[i]]=1;
que.push(to[i]);
}
}
}
}
int main(){
scanf("%d",&t);
while(t--){
tot=0;
memset(head,0,sizeof(head));
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++){
scanf("%s",s);
for(int j=0;j<c;j++)
if(s[j]==‘/‘){
add((i-1)*(c+1)+j+2,i*(c+1)+j+1,0);
add((i-1)*(c+1)+j+1,i*(c+1)+j+2,1);
}
else{
add((i-1)*(c+1)+j+1,i*(c+1)+j+2,0);
add((i-1)*(c+1)+j+2,i*(c+1)+j+1,1);
}
}
spfa(1);
if(dis[(r+1)*(c+1)]==0x3f3f3f3f) cout<<"NO SOLUTION"<<endl;
else cout<<dis[(r+1)*(c+1)]<<endl;
}
}
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 1050010
using namespace std;
struct nond{
int number,dis;
bool operator < (nond b) const{
return dis>b.dis;
}
};
char s[1010];
int t,r,c,tot;
int dis[MAXN];
int to[MAXN*2],net[MAXN*2],cap[MAXN*2],head[MAXN*2];
void add(int u,int v,int w){
to[++tot]=v;net[tot]=head[u];cap[tot]=w;head[u]=tot;
to[++tot]=u;net[tot]=head[v];cap[tot]=w;head[v]=tot;
}
void dijkstra(int s){
priority_queue<nond>que;
memset(dis,0x3f,sizeof(dis));
que.push((nond){s,0});
dis[s]=0;
while(!que.empty()){
nond now=que.top();
que.pop();
if(dis[now.number]!=now.dis) continue;
for(int i=head[now.number];i;i=net[i])
if(dis[to[i]]>dis[now.number]+cap[i]){
dis[to[i]]=dis[now.number]+cap[i];
que.push((nond){to[i],dis[to[i]]});
}
}
}
int main(){
scanf("%d",&t);
while(t--){
tot=0;
memset(head,0,sizeof(head));
scanf("%d%d",&r,&c);
for(int i=1;i<=r;i++){
scanf("%s",s+1);
for(int j=1;j<=c;j++)
if(s[j]==‘/‘){
add((i-1)*(c+1)+j+1,i*(c+1)+j,0);
add((i-1)*(c+1)+j,i*(c+1)+j+1,1);
}
else{
add((i-1)*(c+1)+j,i*(c+1)+j+1,0);
add((i-1)*(c+1)+j+1,i*(c+1)+j,1);
}
}
dijkstra(1);
if(dis[(r+1)*(c+1)]==0x3f3f3f3f) cout<<"NO SOLUTION"<<endl;
else cout<<dis[(r+1)*(c+1)]<<endl;
}
}