#include <bits/stdc++.h>
using namespace std;
int k;
int ai[55][55], bi[55], ci[55];
char di[55];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int m, n;
    cin >> m;
    int i, j;
    for (i = 0; i < m; i++) {
      cin >> di;
      for (j = 0; j < m; j++) {
        ai[i][j] = di[j] - '0';
      }
    }
    memset(bi, 0, sizeof(bi));
    for (i = 0; i < m; i++) {
      ci[i] = 1;
    }
    for (i = m - 1; i >= 0; i--) {
      for (j = m - 1; j >= 0; j--) {
        if (ci[j] == 1) {
          if (ai[i][j] == 1) {
            bi[j] = 1;
            if (j - 1 >= 0) ci[j - 1] = 1;
          } else
            bi[j] = 0;
        } else {
          if (ai[i][j] == 1) {
            break;
          }
        }
      }
      if (j >= 0) {
        break;
      }
      for (j = 0; j < m; j++) {
        ci[j] = bi[j];
      }
      memset(bi, 0, sizeof(bi));
      ci[m - 1] = 1;
    }
    if (i >= 0) {
      printf("NO\n");
    } else {
      printf("YES\n");
    }
  }
  return 0;
}
