#include <bits/stdc++.h>
using namespace std;
#define pb push_back
typedef long long ll;
char a[1005][1005];
int mp[1005][1005];
int vis[1004][1005];
int dist[1005][1005];
int dx[4]={1,-1,0,0};
int dy[4]={0,0,-1,1};
int t,r,c;
string s;
int valid(int x,int y)
{
if(x>=r || x<0 || y>=c || y<0)
{
return 0;
}
if(vis[x][y])
return 0;
return 1;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
deque<pair<int,pair<int,int> > >dq;
scanf("%d",&t);
//cin>>t;
for(int i=0;i<t;i++)
{
dq.clear();
scanf("%d %d",&r,&c);
// cout<<r<<" "<<c<<endl;
// cin>>r>>c;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
vis[i][j]=0;
//dist[i][j]=0;
}
}
for(int j=0;j<r;j++)
{
//for(int k=0;k<c;k++)
//{
scanf("%s",a[j]);
// cout<<"chars "<<a[j][k];
//cin>>a[j][k];
// }
}
for(int j=0;j<r;j++)
{
for(int k=0;k<c;k++)
{
// cout<<a[j][k]<<" ";
}
//cout<<endl;
}
dq.push_back({0,{0,0}});
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
dist[i][j]=INT_MAX;
}
}
dist[0][0]=0;
while(!dq.empty())
{
pair<int,pair<int,int> >p = dq.front();
dq.pop_front();
int x = p.first;
int y = p.second.first;
if(x==r-1 && y==c-1)
break;
int wt = p.second.second;
vis[x][y]=1;
int X,Y;
for(int i=0;i<4;i++)
{
X = x+dx[i];
Y = y+dy[i];
if(valid(X,Y))
{
if(a[X][Y]==a[x][y])
{
dq.push_front({X,{Y,wt}});
dist[X][Y] = min(dist[X][Y],wt);
}
else
{
dq.push_back({X,{Y,wt+1}});
dist[X][Y] = min(dist[X][Y],wt+1);
}
}
}
}
// cout<<"reached"<<endl;
//bfs(r,c);
// cout<<dist[r-1][c-1]-1<<"\n";
printf("%d\n",dist[r-1][c-1]);
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgcGIgcHVzaF9iYWNrCnR5cGVkZWYgbG9uZyBsb25nIGxsOwpjaGFyIGFbMTAwNV1bMTAwNV07CmludCBtcFsxMDA1XVsxMDA1XTsKaW50IHZpc1sxMDA0XVsxMDA1XTsKaW50IGRpc3RbMTAwNV1bMTAwNV07CmludCBkeFs0XT17MSwtMSwwLDB9OwppbnQgZHlbNF09ezAsMCwtMSwxfTsKaW50IHQscixjOwpzdHJpbmcgczsKaW50IHZhbGlkKGludCB4LGludCB5KQp7CiAgICBpZih4Pj1yIHx8IHg8MCB8fCB5Pj1jIHx8IHk8MCkKICAgIHsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGlmKHZpc1t4XVt5XSkKICAgICAgICByZXR1cm4gMDsKICAgIHJldHVybiAxOwp9CgppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwogICAgY2luLnRpZShOVUxMKTsKICAgIGRlcXVlPHBhaXI8aW50LHBhaXI8aW50LGludD4gPiA+ZHE7CiAgICBzY2FuZigiJWQiLCZ0KTsKICAgIC8vY2luPj50OwogICAgZm9yKGludCBpPTA7aTx0O2krKykKICAgIHsKICAgICAgICBkcS5jbGVhcigpOwogICAgICAgIHNjYW5mKCIlZCAlZCIsJnIsJmMpOwogICAgICAgLy8gY291dDw8cjw8IiAiPDxjPDxlbmRsOwogICAgICAvLyAgY2luPj5yPj5jOwogICAgICAgIGZvcihpbnQgaT0wO2k8cjtpKyspewogICAgICAgICAgICBmb3IoaW50IGo9MDtqPGM7aisrKXsKICAgICAgICAgICAgICAgIHZpc1tpXVtqXT0wOwogICAgICAgICAgICAgICAgLy9kaXN0W2ldW2pdPTA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yKGludCBqPTA7ajxyO2orKykKICAgICAgICB7CiAgICAgICAgICAgIC8vZm9yKGludCBrPTA7azxjO2srKykKICAgICAgICAgICAgLy97CiAgICAgICAgICAgICAgICBzY2FuZigiJXMiLGFbal0pOwogICAgICAgICAgICAgIC8vICBjb3V0PDwiY2hhcnMgIjw8YVtqXVtrXTsKICAgICAgICAgICAgICAgIC8vY2luPj5hW2pdW2tdOwogICAgICAgICAgIC8vIH0KICAgICAgICB9CiAgICAgICAgZm9yKGludCBqPTA7ajxyO2orKykKICAgICAgICB7CiAgICAgICAgICAgIGZvcihpbnQgaz0wO2s8YztrKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgIC8vICAgY291dDw8YVtqXVtrXTw8IiAiOwogICAgICAgICAgICB9CiAgICAgICAgICAgIC8vY291dDw8ZW5kbDsKICAgICAgICB9CiAgICBkcS5wdXNoX2JhY2soezAsezAsMH19KTsKCiAgICBmb3IoaW50IGk9MDtpPHI7aSsrKQogICAgewogICAgICAgIGZvcihpbnQgaj0wO2o8YztqKyspCiAgICAgICAgewogICAgICAgICAgICBkaXN0W2ldW2pdPUlOVF9NQVg7CiAgICAgICAgfQogICAgfQoKICAgIGRpc3RbMF1bMF09MDsKICAgIHdoaWxlKCFkcS5lbXB0eSgpKQogICAgewogICAgICAgIHBhaXI8aW50LHBhaXI8aW50LGludD4gPnAgPSBkcS5mcm9udCgpOwogICAgICAgIGRxLnBvcF9mcm9udCgpOwogICAgICAgIGludCB4ID0gcC5maXJzdDsKICAgICAgICBpbnQgeSA9IHAuc2Vjb25kLmZpcnN0OwogICAgICAgIGlmKHg9PXItMSAmJiB5PT1jLTEpCiAgICAgICAgCWJyZWFrOwogICAgICAgIGludCB3dCA9IHAuc2Vjb25kLnNlY29uZDsKICAgICAgICB2aXNbeF1beV09MTsKICAgICAgICBpbnQgWCxZOwogICAgICAgIGZvcihpbnQgaT0wO2k8NDtpKyspCiAgICAgICAgewogICAgICAgICAgICBYID0geCtkeFtpXTsKICAgICAgICAgICAgWSA9IHkrZHlbaV07CiAgICAgICAgICAgIGlmKHZhbGlkKFgsWSkpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKGFbWF1bWV09PWFbeF1beV0pCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgZHEucHVzaF9mcm9udCh7WCx7WSx3dH19KTsKICAgICAgICAgICAgICAgICAgICBkaXN0W1hdW1ldID0gbWluKGRpc3RbWF1bWV0sd3QpOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGRxLnB1c2hfYmFjayh7WCx7WSx3dCsxfX0pOwogICAgICAgICAgICAgICAgICAgIGRpc3RbWF1bWV0gPSBtaW4oZGlzdFtYXVtZXSx3dCsxKTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgLy8gY291dDw8InJlYWNoZWQiPDxlbmRsOwogICAgICAgIC8vYmZzKHIsYyk7CiAgICAgLy8gICBjb3V0PDxkaXN0W3ItMV1bYy0xXS0xPDwiXG4iOwogICAgICAgIHByaW50ZigiJWRcbiIsZGlzdFtyLTFdW2MtMV0pOwogICAgfQogICAgcmV0dXJuIDA7Cn0K