#include <bits/stdc++.h>
using namespace std;
#define debug(a) cerr << #a << " = " << (a) << nl;
#define ll long long
#define int long long
#define nl '\n'
const int N = 1e3+5;
int n, m;
int a[N][N];
// path count, defuse
pair<int, int> go(int i, int j, int from, int cons) {
if(i == 1 and j == 1) return make_pair(1, 0);
if(i < 1 or j < 1) return make_pair(0, 1e8);
int def = 0;
if(from == 1) {
if(cons == 1) def += a[i-1][j] + a[i][j-1];
else def += a[i-1][j] + a[i][j+1] + a[i][j-1];
}
else {
if(cons == 1) def += a[i][j-1] + a[i-1][j];
else def += a[i-1][j] + a[i+1][j] + a[i-1][j];
}
// cerr << i << " " << j << ' ' << from << ' ' << cons << " " << def << endl;
auto [p1, q1] = go(i-1, j, 1, 1+(from==1)); // top
auto [p2, q2] = go(i, j-1, 0, 1+(from==0)); // left
if(q1 == q2) return make_pair(p1+p2, q1+def);
else if(q1 < q2) return make_pair(p1, q1+def);
else return make_pair(p2, q2+def);
}
void jAVA()
{
cin >> n >> m;
for(int i=0; i<=n+2;++i) {
for(int j=0; j<=m+2;++j) {
a[i][j] = 0;
}
}
for (int i = 1; i <= n; i++) {
string s; cin >> s;
for (int j = 0; j < m; j++) {
if(s[j] == '#') a[i][j+1] = 1;
}
}
a[1][1] = 0;
a[1][2] = 0;
a[2][1] = 0;
a[n][m] = 0;
a[n-1][m] = 0;
a[n][m-1] = 0;
// for(int i=1; i<=n;++i) {
// for(int j=1; j<=m;++j) {
// cout << a[i][j] << ' ';
// }
// cout << '\n';
// }
cout << go(n,m,0,0).first << nl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1, cs = 0;
cin >> t;
while (t--){
// cout << "Case " << ++cs << ": ";
jAVA();
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgZGVidWcoYSkgY2VyciA8PCAjYSA8PCAiID0gIiA8PCAoYSkgPDwgbm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bWFrZV9wYWlyKHAxLCBxMStkZWYpOwogICAgZWxzZSByZXR1cm4gbWFrZV9wYWlyKHAyLCBxMitkZWYpOwp9Cgp2b2lkIGpBVkEoKQp7CiAgICBjaW4gPj4gbiA+PiBtOwogICAgZm9yKGludCBpPTA7IGk8PW4rMjsrK2kpIHsKICAgICAgICBmb3IoaW50IGo9MDsgajw9bSsyOysraikgewogICAgICAgICAgICBhW2ldW2pdID0gMDsKICAgICAgICB9CiAgICB9CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKICAgICAgICBzdHJpbmcgczsgY2luID4+IHM7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBtOyBqKyspIHsKICAgICAgICAgICAgaWYoc1tqXSA9PSAnIycpIGFbaV1baisxXSA9IDE7CiAgICAgICAgfQogICAgfQogICAgYVsxXVsxXSA9IDA7CiAgICBhWzFdWzJdID0gMDsKICAgIGFbMl1bMV0gPSAwOwoKICAgIGFbbl1bbV0gPSAwOwogICAgYVtuLTFdW21dID0gMDsKICAgIGFbbl1bbS0xXSA9IDA7CiAgICAvLyBmb3IoaW50IGk9MTsgaTw9bjsrK2kpIHsKICAgIC8vICAgICBmb3IoaW50IGo9MTsgajw9bTsrK2opIHsKICAgIC8vICAgICAgICAgY291dCA8PCBhW2ldW2pdIDw8ICcgJzsKICAgIC8vICAgICB9CiAgICAvLyAgICAgY291dCA8PCAnXG4nOwogICAgLy8gfQogICAgY291dCA8PCBnbyhuLG0sMCwwKS5maXJzdCA8PCBubDsKfQoKaW50MzJfdCBtYWluKCkKewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbyhmYWxzZSk7CiAgICBjaW4udGllKG51bGxwdHIpOyBjb3V0LnRpZShudWxscHRyKTsKCiAgICBpbnQgdCA9IDEsIGNzID0gMDsKICAgIGNpbiA+PiB0OwogICAgd2hpbGUgKHQtLSl7CiAgICAgICAgLy8gY291dCA8PCAiQ2FzZSAiIDw8ICsrY3MgPDwgIjogIjsKICAgICAgICBqQVZBKCk7CiAgICB9CiAgICByZXR1cm4gMDsKfQ==