#include<bits/stdc++.h>
using namespace std;

typedef long long int ll;
#define IOS ios_base::sync_with_stdio(0);  cin.tie(0); cout.tie(0);

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>order_set;
typedef pair<int, int>pr;
#define all(i)     i.begin() , i.end()
#define ft     first
#define sn     second
#define pb push_back

#define totalone(mask) __builtin_popcount(mask)
#define chkbit(mask,bit) (mask&(1LL << bit))
#define setbit(mask,bit) (mask|(1LL << bit)
#define cngbit(mask,bit) (mask^(1LL << bit))

// debug section start
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}

template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// debug section closed

#define en "\n"
#define dbg(x) cerr<<#x<<" is : "<<x<<en;
#define sum(n) ((1LL*(n)*(n+1))/ 2LL)
#define sqr(n) (1LL*(n)*(n))

#define vag(a,b) ((a + b - 1)/b)

#define MAXN 2010
#define inf 1e6+5
const ll mod = 1000000007;

int n, m;
string a[1001];

int dx[] = { -1, 1, 0, 0};
int dy[] = {0, 0, -1, 1};

bool valid(int x, int y)
{
    if (x >= 0 && x < n &&  x >= 0 && y < m) return true;
    return false;
}
void bfs(int x, int y)
{
    deque<pair<int, int>>dq;

    vector<vector<int>>dis(n, vector<int>(m, -1));

    dis[x][y] = 0;
    dq.push_back({x, y});
    while (!dq.empty())
    {
        x = dq.front().ft; y = dq.front().sn;
        dq.pop_front();

        for (int i = 0; i < 4; i++) {
            int tx = x + dx[i];
            int ty = y + dy[i];

            if (!valid(tx, ty)) continue;

            if (a[x][y] != a[tx][ty]) {
                if (dis[tx][ty] != -1) continue;
                dis[tx][ty] = dis[x][y] + 1;
                dq.push_back({tx, ty});
            }
            else {
                if (dis[tx][ty] != -1 && dis[tx][ty] <= dis[x][y]) continue;
                dis[tx][ty] = dis[x][y];
                dq.push_front({tx, ty});
            }

        }
    }

    cout << dis[n - 1][m - 1] << en;

}
void solve()
{
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    bfs(0, 0);
}
int main()
{
    IOS;
    ll t;
    t = 1;
    cin >> t;

    int c = 0;
    while ( t-- )
    {
        // cout<<"Case "<<++c<<": ";
        solve();
    }
    return 0;
}