#include <bits/stdc++.h>
using namespace std;
long long mod = 1000000007;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  for (int l = 0; l < t; l++) {
    int n;
    cin >> n;
    vector<string> s(n);
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    vector<vector<int>> dp(n, vector<int>(n, 0));
    for (int i = 0; i < n; i++) {
      if (s[n - 1][i] == '1') dp[n - 1][i] = 1;
    }
    for (int i = 0; i < n; i++) {
      if (s[i][n - 1] == '1') dp[i][n - 1] = 1;
    }
    int t = 1;
    for (int i = n - 2; i >= 0; i--) {
      for (int j = n - 2; j >= 0; j--) {
        if (s[i][j] == '1') {
          dp[i][j] = dp[i + 1][j] || dp[i][j + 1];
          if (!dp[i][j]) {
            t = 0;
            break;
          }
        }
      }
      if (t == 0) break;
    }
    if (t)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
