#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<char>>& a, vector<vector<bool>>& check, long long int i,
         long long int j) {
  check[i][j] = 1;
  if (j - 1 >= 0 && !check[i][j - 1] && a[i][j - 1] == '1')
    dfs(a, check, i, j - 1);
  if (i - 1 >= 0 && !check[i - 1][j] && a[i - 1][j] == '1')
    dfs(a, check, i - 1, j);
}
void print(vector<vector<bool>> check) {
  for (long long int i = 0; i < check.size(); i++) {
    for (long long int j = 0; j < check[i].size(); j++)
      cout << check[i][j] << " ";
    cout << "\n";
  }
}
int32_t main() {
  long long int t;
  cin >> t;
  while (t--) {
    long long int n;
    cin >> n;
    vector<vector<char>> a(n, vector<char>(n));
    vector<vector<bool>> check(n, vector<bool>(n));
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) cin >> a[i][j];
    }
    bool flag = 1;
    for (long long int i = 0; i < n; i++) {
      if (a[i][n - 1] == '1') dfs(a, check, i, n - 1);
    }
    for (long long int i = 0; i < n; i++) {
      if (a[n - 1][i] == '1' && !check[n - 1][i]) dfs(a, check, n - 1, i);
    }
    for (long long int i = 0; i < n; i++) {
      for (long long int j = 0; j < n; j++) {
        if (a[i][j] == '1' && !check[i][j]) {
          flag = 0;
          break;
        }
      }
      if (!flag) break;
    }
    if (flag)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
}
