#include <bits/stdc++.h>
using namespace std;
void fopen() {
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
}
int a[50][50];
int n;
int ans[2500];
vector<int> v[50 * 50 + 1];
void bfs(int i, int j) {
  queue<int> q;
  int vis[n * n];
  q.push(i * n + j);
  memset(vis, 0, sizeof(vis));
  ;
  vis[i * n + j] = 1;
  ans[i * n + j] = 1;
  while (!q.empty()) {
    int p = q.front();
    q.pop();
    for (auto x : v[p]) {
      if (vis[x] == 0) {
        vis[x] = 1;
        ans[x] = 1;
        q.push(x);
      }
    }
  }
}
void sohuken() {
  cin >> n;
  char c;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> c;
      a[i][j] = c - 48;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 1) {
        if (i + 1 < n && a[i + 1][j] == 1) {
          v[(i + 1) * n + j].push_back(i * n + j);
        }
        if (j + 1 < n && a[i][j + 1] == 1) {
          v[i * n + j + 1].push_back(i * n + j);
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (a[i][n - 1] == 1) {
      bfs(i, n - 1);
    }
    if (a[n - 1][i] == 1) {
      bfs(n - 1, i);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (a[i][j] == 1) {
        if (ans[i * n + j] == 0) {
          cout << "NO";
          return;
        }
      }
    }
  }
  cout << "YES";
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    sohuken();
    memset(ans, 0, sizeof(ans));
    ;
    for (int i = 0; i < 50 * 50 + 1; i++) {
      v[i].clear();
    }
    cout << endl;
  }
}
