#include <bits/stdc++.h>
using namespace std;
void solve() {
  int n;
  cin >> n;
  vector<vector<int> > grid(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      char c;
      cin >> c;
      if (c == '1') {
        grid[i][j] = 1;
      }
    }
  }
  vector<vector<int> > rrow(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    rrow[i][n - 1] = grid[i][n - 1] == 1 ? 1 : 0;
    for (int j = n - 2; j >= 0; j--) {
      rrow[i][j] = (rrow[i][j + 1] == 1 && grid[i][j] == 1) ||
                           (grid[i][j] == 1 && grid[i][j + 1] == 1)
                       ? 1
                       : 0;
    }
  }
  vector<vector<int> > ccol(n, vector<int>(n, 0));
  for (int j = 0; j < n; j++) {
    ccol[n - 1][j] = grid[n - 1][j] == 1 ? 1 : 0;
    for (int i = n - 2; i >= 0; i--) {
      ccol[i][j] = (ccol[i + 1][j] == 1 && grid[i][j] == 1) ||
                           (grid[i][j] == 1 && grid[i + 1][j] == 1)
                       ? 1
                       : 0;
    }
  }
  if (n == 2) {
    bool isPos = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (grid[i][j] == 1) {
          if (rrow[i][j] == 0 && ccol[i][j] == 0) {
            isPos = false;
            break;
          }
        }
      }
    }
    if (isPos) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
    return;
  }
  bool isPos = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (grid[i][j] == 1) {
        if (rrow[i][j] == 0 && ccol[i][j] == 0) {
          isPos = false;
          break;
        }
      }
    }
  }
  if (isPos) {
    cout << "YES" << endl;
  } else {
    cout << "NO" << endl;
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
