#include <bits/stdc++.h>
using namespace std;
int main() {
  int t, n;
  string s;
  bitset<2500> outside, polygon, match;
  cin >> t;
  for (int i = 0; i < t; i++) {
    cin >> n;
    polygon.reset();
    outside.reset();
    for (int j = 0; j < n; j++) {
      cin >> s;
      for (int k = 0; k < n; k++) {
        if (s[k] == '1') polygon.set((j * n) + k);
        if (j == (n - 1) || k == (n - 1)) outside.set((j * n) + k);
      }
    }
    match = (polygon >> n & polygon) | (polygon >> 1 & polygon) |
            (outside & polygon);
    if (match != polygon)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
  return EXIT_SUCCESS;
}
