#include <bits/stdc++.h>
using namespace std;
bool debug_disabled = true;
int dp[51][51];
int board[51][51];
void solve() {
  memset(dp, 0, sizeof dp);
  memset(board, 0, sizeof board);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    for (int j = 0; j < n; ++j)
      if (s[j] == '1') board[i][j] = 1;
  }
  for (int i = 0; i < n; ++i) {
    if (board[i][n - 1] == 1) dp[i][n - 1] = 1;
    if (board[n - 1][i] == 1) dp[n - 1][i] = 1;
  }
  for (int i = n - 2; i >= 0; --i) {
    for (int j = i; j >= 0; --j) {
      if (board[i][j] == 1) {
        if (dp[i + 1][j] == 1 || dp[i][j + 1] == 1) dp[i][j] = 1;
      }
      if (board[j][i] == 1) {
        if (dp[j + 1][i] == 1 || dp[j][i + 1] == 1) dp[j][i] = 1;
      }
    }
  }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < n; ++j)
      if (board[i][j] == 1 && dp[i][j] == 0) {
        if (debug_disabled) {
        } else
          cerr << "i = " << i << ", j = " << j << endl;
        cout << "NO" << endl;
        return;
      }
  cout << "YES" << endl;
}
int main() {
  cout.setf(ios::fixed, ios::floatfield);
  cout.setf(ios::showpoint);
  cout.precision(12);
  cin.tie(0);
  cout.tie(0);
  ios::sync_with_stdio(false);
  int T;
  cin >> T;
  for (int i = 1; i <= T; ++i) {
    solve();
  }
  return 0;
}
