#include <bits/stdc++.h>
using namespace std;
void solve();
int main() {
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
void solve() {
  int n;
  cin >> n;
  queue<pair<int, int> > help;
  char** a = new char*[n];
  for (int i = 0; i < n; i++) {
    a[i] = new char[n];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      if (a[i][j] == '1') {
        help.push(make_pair(i, j));
      }
    }
  }
  while (help.size()) {
    pair<int, int> shot = help.front();
    help.pop();
    if (shot.first == n - 1 || shot.second == n - 1 ||
        a[shot.first + 1][shot.second] == '1' ||
        a[shot.first][shot.second + 1] == '1') {
      continue;
    } else {
      cout << "NO" << endl;
      return;
    }
  }
  cout << "YES" << endl;
}
