#include <bits/stdc++.h>
using namespace std;
const int N = 1e6;
int n;
bool solve() {
  cin >> n;
  string a[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  int seen[n][n];
  memset(seen, 0, sizeof seen);
  queue<pair<int, int> > q;
  for (int i = 0; i < n; i++) {
    if (a[n - 1][i] == '1') q.push({n - 1, i});
    if (a[i][n - 1] == '1') q.push({i, n - 1});
  }
  while (((int)(q.size()))) {
    int x = q.front().first;
    int y = q.front().second;
    q.pop();
    if (x < 0 || y < 0) continue;
    if (seen[x][y]) continue;
    if (a[x][y] == '0') continue;
    seen[x][y] = 1;
    if (x > 0 && !seen[x - 1][y] && a[x - 1][y] == '1') q.push({x - 1, y});
    if (y > 0 && !seen[x][y - 1] && a[x][y - 1] == '1') q.push({x, y - 1});
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == '1' && !seen[i][j]) {
        return 0;
      }
    }
  }
  return 1;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  cin >> tc;
  while (tc--) {
    cout << (solve() ? "YES" : "NO") << '\n';
  }
  return 0;
}
