#include <bits/stdc++.h>
using namespace std;
bool check(int i, int j, int n) {
  if (i >= 0 && i < n && j >= 0 && j < n)
    return 1;
  else
    return 0;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, mini = 10000000000, k, ans = 0;
    cin >> n;
    char c[n][n];
    queue<pair<int, int>> s;
    pair<int, int> top;
    for (auto i = 0; i < n; i++) {
      for (auto j = 0; j < n; j++) cin >> c[i][j];
    }
    for (auto i = 0; i < n; i++) {
      if (c[i][n - 1] == '1') {
        s.push({i, n - 1});
        c[i][n - 1] = '2';
        while (!s.empty()) {
          top = s.front();
          s.pop();
          int i1 = top.first;
          int j1 = top.second;
          if (check(i1, j1 - 1, n - 1) && c[i1][j1 - 1] == '1') {
            s.push({i1, j1 - 1});
            c[i1][j1 - 1] = '2';
          }
          if (check(i1 - 1, j1, n - 1) && c[i1 - 1][j1] == '1') {
            s.push({i1 - 1, j1});
            c[i1 - 1][j1] = '2';
          }
        }
      }
    }
    for (auto i = 0; i < n; i++) {
      if (c[n - 1][i] == '1') {
        s.push({n - 1, i});
        c[n - 1][i] = '2';
        while (!s.empty()) {
          top = s.front();
          s.pop();
          int i1 = top.first;
          int j1 = top.second;
          if (check(i1, j1 - 1, n - 1) && c[i1][j1 - 1] == '1') {
            s.push({i1, j1 - 1});
            c[i1][j1 - 1] = '2';
          }
          if (check(i1 - 1, j1, n - 1) && c[i1 - 1][j1] == '1') {
            s.push({i1 - 1, j1});
            c[i1 - 1][j1] = '2';
          }
        }
      }
    }
    ans = 0;
    for (auto i = 0; i < n; i++) {
      for (auto j = 0; j < n; j++) {
        if (c[i][j] == '1') ans = 1;
      }
    }
    if (ans)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
}
