#include<bits/stdc++.h>
#define int long long
using namespace std;
struct node{int x,y,step;};
int T,n,m,flag;
char a[2010][2010],way[2010][2010];
bool vis[2010][2010];
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
signed main(){
cin>>T;
while(T--){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
vis[i][j]=false;
way[i][j]=' ';
}
}
flag=0;
queue<node> q;
q.push(node{1,1,0});
vis[1][1]=1;
while(!q.empty()){
node t=q.front();
q.pop();
if(t.x==n&&t.y==m){
cout<<t.step<<endl;
flag=1;
break;
}
for(int i=0;i<4;i++){
int tx=t.x+dx[i],ty=t.y+dy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=m&&!vis[tx][ty]&&a[tx][ty]!=a[t.x][t.y]){
if(tx==t.x+1&&ty==t.y)way[tx][ty]='D';
if(tx==t.x-1&&ty==t.y)way[tx][ty]='U';
if(tx==t.x&&ty==t.y+1)way[tx][ty]='R';
if(tx==t.x&&ty==t.y-1)way[tx][ty]='L';
q.push(node{tx,ty,t.step+1});
}
}
}
if(flag==0){
cout<<-1<<endl;
continue;
}
/*for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cout<<way[i][j]<<" ";
}
cout<<endl;
}*/
stack<char> s;
for(int i=n,j=m;;){
if(i==1&&j==1)break;
s.push(way[i][j]);
if(way[i][j]=='D')i=i-1;
else if(way[i][j]=='U')i=i+1;
else if(way[i][j]=='R')j=j-1;
else if(way[i][j]=='l')j=j+1;
}
while(!s.empty()){cout<<s.top();s.pop();}
cout<<endl;
}
return 0;
}