#include #include #include #include #include using namespace std; #define LL long long #define DB double template void read (T &x) { x = 0; T f = 1; char ch = getchar (); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar (); } while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + ch - '0'; ch = getchar (); } x *= f; } template void write (T x) { if (x < 0) { x = -x; putchar ('-'); } if (x < 10) { putchar (x + '0'); return; } write (x / 10); putchar (x % 10 + '0'); } template void print (T x, char ch) { write (x); putchar (ch); } template T Min (T x, T y) { return x < y ? x : y; } template T Max (T x, T y) { return x > y ? x : y; } template T Abs (T x) { return x > 0 ? x : -x; } const int Maxn = 10; int t, n; char c[Maxn + 5][Maxn + 5]; bool dp[Maxn + 5][Maxn + 5][2 * Maxn + 5]; int tox[10] = { 0, 1, 0 }; int toy[10] = { 0, 0, 1 }; bool vis[Maxn + 5][Maxn + 5]; bool BFS () { memset (vis, 0, sizeof vis); queue > q; q.push (make_pair (1, 1)); vis[1][1] = 1; while (q.size ()) { pair tmp = q.front (); q.pop (); int x = tmp.first, y = tmp.second; if (x == n && y == n) return 1; for (int i = 1; i <= 2; i++) { int nx = x + tox[i], ny = y + toy[i]; if (nx < 1 || nx > n) continue; if (ny < 1 || ny > n) continue; if (c[nx][ny] == '#') continue; if (vis[nx][ny] == 1) continue; vis[nx][ny] = 1; q.push (make_pair (nx, ny)); } } return 0; } int main () { read (t); while (t--) { read (n); for (int i = 1; i <= n; i++) scanf ("%s", c[i] + 1); if (c[1][1] == '#' || c[n][n] == '#') { printf ("0\n"); continue; } memset (dp, 0, sizeof dp); dp[1][1][1] = 1; for (int i = 1; i <= n; i++) { for (int x = 1; x <= n; x++) { for (int k = 1; k <= 2 * n - 1; k++) { int j = k - i + 1, y = k - x + 1; if (j < 1 || j > n) continue; if (y < 1 || y > n) continue; if (dp[i][x][k] == 0) continue; if (c[i + 1][j] == '.' && c[x + 1][y] == '.' && (i + 1 != x + 1 || k + 1 == 2 * n - 1)) dp[i + 1][x + 1][k + 1] |= 1; if (c[i + 1][j] == '.' && c[x][y + 1] == '.' && (i + 1 != x || k + 1 == 2 * n - 1)) dp[i + 1][x][k + 1] |= 1; if (c[i][j + 1] == '.' && c[x + 1][y] == '.' && (i != x + 1 || k + 1 == 2 * n - 1)) dp[i][x + 1][k + 1] |= 1; if (c[i][j + 1] == '.' && c[x][y + 1] == '.' && (i != x || k + 1 == 2 * n - 1)) dp[i][x][k + 1] |= 1; } } } // for (int k = 1; k <= n; k++) // for (int i = 1; i <= n; i++) // for (int j = 1; j <= n; j++) // if (dp[i][j][k]) // printf ("dp[%d][%d][%d][%d] = %d\n", i, k - i + 1, j, k - j + 1, dp[i][j][k]); if (dp[n][n][2 * n - 1]) { printf ("2\n"); continue; } if (BFS ()) printf ("1\n"); else printf ("0\n"); } return 0; }