#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t) {
    int n, m;
    cin >> n;
    vector<vector<int> > v;
    v.resize(n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        char c;
        cin >> c;
        v[i].push_back(c - '0');
      }
    }
    bool test = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (v[i][i] == 1) {
        if (i + 1 >= n || v[i + 1][i] == 1 || v[i][i + 1] == 1) {
          test = 1;
        } else {
          test = 0;
          break;
        }
      }
      for (int j = i; j >= 0; j--) {
        if (v[i][j] == 1) {
          if (i + 1 >= n || j + 1 >= n || v[i][j + 1] == 1 ||
              v[i + 1][j] == 1) {
            test = 1;
          } else {
            test = 0;
            break;
          }
        }
        if (v[j][i] == 1) {
          if (i + 1 >= n || j + 1 >= n || v[j + 1][i] == 1 ||
              v[j][i + 1] == 1) {
            test = 1;
          } else {
            test = 0;
            break;
          }
        }
      }
      if (test == 0) break;
    }
    if (test == 1)
      cout << "YES\n";
    else
      cout << "NO\n";
    t--;
  }
}
