#include<bits/stdc++.h>
using namespace std;
int s, n, m, h, t, q[40005][3];
int fx[5] = {0, 0, 1, 0, -1};
int fy[5] = {0, 1, 0, -1, 0};
int tx, ty;
bool f[205][205], r;
char a[205][205];
int sun;
int main() {
cin >> s;
while (s) {
h = 1;
t = 1;
sun = 0;
memset(f, 0, sizeof(f));
int x, y, xx, yy;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> a[i][j];
if (a[i][j] == 'r') {
x = i;
y = j;
}
if(a[i][j] == 'a'){
xx = i;
yy = j;
}
if(a[i][j] == '#')f[i][j]=1;
}
}
q[1][1] = x;
q[1][2] = y;
f[x][y] = 1;
while (h <= t) {
for (int i = 1; i <= 4; i++) {
tx = q[h][1] + fx[i];
tx = q[h][2] + fy[i];
if (tx >= 1 && tx <= n && ty >= 1 && ty <= m && !f[tx][ty] && a[tx][ty] != '#') {
t++;
q[t][1] = tx;
q[t][2] = ty;
sun++;
if (a[tx][ty] == 'x')sun++;
f[tx][ty] = 1;
}
if (a[q[t][1]][q[t][2]] == 'a') {
r = 1;
h=t+1;
break;
}
}
h++;
}
if (r == 1)cout << sun << '\n';
else cout << "Impossible" << '\n';
r = 0;
s--;
}
return 0;
}
谢谢了大佬