#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t = 1, i, j, k;
  cin >> t;
  for (long long ii = 1; ii <= t; ii++) {
    long long n;
    cin >> n;
    vector<string> a(n);
    for (i = 0; i < n; i++) cin >> a[i];
    queue<pair<long long, long long> > q;
    vector<vector<long long> > st(n, vector<long long>(n, 0));
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (a[i][j] == '0') continue;
        if (st[i][j] == 0) q.push(make_pair(i, j));
        st[i][j] == 1;
        break;
      }
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (a[j][i] == '0') continue;
        if (st[j][i] == 0) q.push(make_pair(j, i));
        st[j][i] == 1;
        break;
      }
    }
    while (!q.empty()) {
      pair<long long, long long> x = q.front();
      k = 0;
      if (x.first != n - 1) {
        if (a[x.first + 1][x.second] == '1') {
          k = 1;
          if (st[x.first + 1][x.second] == 0)
            q.push(make_pair(x.first + 1, x.second));
          st[x.first + 1][x.second] = 1;
        }
      } else
        k = 1;
      if (x.second != n - 1) {
        if (a[x.first][x.second + 1] == '1') {
          k = 1;
          if (st[x.first][x.second + 1] == 0)
            q.push(make_pair(x.first, x.second + 1));
          st[x.first][x.second + 1] = 1;
        }
      } else
        k = 1;
      if (k == 0) break;
      q.pop();
    }
    if (!q.empty())
      cout << "NO" << '\n';
    else
      cout << "YES" << '\n';
  }
  return 0;
}
