#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0), cin.tie(NULL);
  cout.tie(NULL);
  int t, p;
  cin >> t;
  for (p = 1; p <= t; p++) {
    int i, j, ans, n, l, r, k;
    char a[500][500];
    cin >> n;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) cin >> a[i][j];
    ans = 1;
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++) {
        if (a[i][j] == '1') {
          l = i, r = j;
          if ((j == n || i == n) || (a[i][j + 1] == '1') ||
              (a[i + 1][j] == '1'))
            a[i][j] = '0';
        }
      }
    for (i = 1; i <= n; i++)
      for (j = 1; j <= n; j++)
        if (a[i][j] == '1') ans = 0;
    if (ans == 1)
      cout << "YES" << '\n';
    else
      cout << "NO" << '\n';
  }
  return 0;
}
