#include <bits/stdc++.h>
using namespace std;
template <typename T>
constexpr string_view gtype() {
  string_view s = __PRETTY_FUNCTION__;
  return string_view(s.data() + 45, s.find(';', 45) - 45);
}
char mat[50][50];
bool vis[50][50];
int dx[2] = {0, -1};
int dy[2] = {-1, 0};
int n;
bool isValid(int i, int j) {
  if ((i < 0) || (j < 0) || (i >= n) || (j >= n)) return false;
  return true;
}
void dfs(int i, int j) {
  if (vis[i][j]) return;
  vis[i][j] = true;
  for (int k = 0; k < 2; ++k) {
    int m = i + dx[k];
    int z = j + dy[k];
    if (isValid(m, z) and mat[m][z] == '1') {
      if (!vis[m][z]) dfs(m, z);
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    for (int i = 0; i < 50; ++i) {
      for (int j = 0; j < 50; ++j) vis[i][j] = 0;
    }
    cin >> n;
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j) cin >> mat[i][j];
    for (int i = 0; i < n; ++i) {
      if (mat[i][n - 1] == '1') dfs(i, n - 1);
      if (mat[n - 1][i] == '1') dfs(n - 1, i);
    }
    bool can = true;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if ((mat[i][j] == '1') and (vis[i][j] == false)) {
          can = false;
          break;
        }
      }
    }
    if (can)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
