/** * author: roccoshi * created: 2021-08-07 13:54:30 */ #pragma GCC diagnostic ignored "-Wsign-conversion" #pragma GCC diagnostic ignored "-Wsign-compare" #include using namespace std; #define ll long long #define endl '\n' #define pii pair #define mp make_pair #define pb push_back #define fi first #define se second const int pi = 3.1415927; const int mod = 1e9 + 7; const int inf = 0x3f3f3f3f; const int ninf = 0xc0c0c0c0; const int N = 15; const int M = 200000 + 5; int a[N][N]; int n, ans; // void bfs (int xx, int yy) { // queue> q; // q.emplace(xx, yy); // while (q.size()) { // int sz = q.size(); // for (int i = 0; i < sz; ++i) { // auto now = q.front(); // int x = now.fi, y = now.se; // q.pop(); // if (a[x][y + 1] == 1) { // q.emplace(x, y + 1); // if (x == n && y + 1 == n) { // ++ans; // return; // } // a[x][y + 1] = 2; // } // else if (a[x + 1][y] == 1) { // q.emplace(x + 1, y); // if (x + 1== n && y == n) { // ++ans; // return; // } // a[x + 1][y] = 2; // } // } // } // } // bool se = false; // void dfs (int x, int y) { // bool flag = false; // if (!(x == 1 && y == 1)) a[x][y] = 2; // if (a[x][y + 1] == 1) { // 尽量往右 // flag = true; // if (x == n && y + 1 == n) { // se = true; // ans = 1; return; // } // dfs(x, y + 1); // } // if (!se) { // if (a[x + 1][y] == 1) { // 往下 // flag = true; // if (x + 1 == n && y == n) { // se = true; // ans = 1; return; // } // dfs(x + 1, y); // } // } // if (!flag) a[x][y] = 1; // } // void bfs_1 (int xx, int yy) { // queue> q; // q.emplace(xx, yy); // while (q.size()) { // int sz = q.size(); // for (int i = 0; i < sz; ++i) { // auto now = q.front(); // int x = now.fi, y = now.se; // if (x == n && y == n) { // ++ans; // return; // } // q.pop(); // if (a[x + 1][y] == 1) { // q.emplace(x + 1, y); // } // else if (a[x][y + 1] == 1) { // q.emplace(x, y + 1); // } // } // } // } void dfs(int x, int y, int cnt) { if (ans == cnt) return; if (x == n && y == n) { ans++; return; } if (a[x][y] == 0) return; dfs(x, y + 1, cnt); a[x][y + 1] = 0; if (ans == cnt) return; dfs(x + 1, y, cnt); a[x + 1][y] = 0; } // void dfs2(int x, int y) { // if (ans == 2) return; // if (x == n && y == n) { // ans = 2; return; // } // if (a[x][y] == 0) return; // dfs(x + 1, y); // a[x + 1][y] = 0; // if (ans == 2) return; // dfs(x, y + 1); // a[x][y + 1] = 0; // } int main() { ios::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { memset(a, 0, sizeof a); cin >> n; char c; for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> c; if (c == '.') a[i][j] = 1; // 可以走 else a[i][j] = 0; } } if (a[1][1] == 0 || a[n][n] == 0) { cout << 0 << endl; continue; } ans = 0; dfs(1, 1, 1); // for (int i = 1; i <= n; ++i) { // for (int j = 1; j <= n; ++j) { // cout << a[i][j] << ' '; // } // cout << endl; // } dfs(1, 1, 2); cout << ans << endl; } return 0; } /* 造数据 8 4 .... .##. .##. .... 4 .... .##. .##. .... 4 .... .##. .##. ..#. 4 .... .##. .### .... 4 .... .##. .### #... 4 .... .##. .##. .... 2 .. .. 5 ..... #..## ##.## ..... ..... expect: 2 2 1 1 0 2 2 1 3 6 ...... #.###. ...... ...... ...... ...... 6 ...... ...... #.###. ...... ...... ...... 6 ...... #####. #.#... ..#.## ..#... ..#... 6 ...... ...... #.#... ..#.## ..#... ..#... */