#include <bits/stdc++.h>
using namespace std;
string military(int n, vector<int> v[]) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    for (j = 0; j < n - 1; j++) {
      if (v[i][j]) {
        if (v[i][j + 1] != 1 && v[i + 1][j] != 1) {
          return "NO";
        }
      }
    }
  }
  return "YES";
}
int main() {
  int t, n, i, j;
  cin >> t;
  while (t--) {
    cin >> n;
    vector<int> v[n];
    for (i = 0; i < n; i++) {
      string s;
      cin >> s;
      for (j = 0; j < n; j++) {
        v[i].push_back(s[j] - '0');
      }
    }
    cout << military(n, v) << '\n';
  }
}
