#include <bits/stdc++.h>
using namespace std;
void dfs(int i, int j, vector<vector<char>>& grid,
         vector<vector<char>>& visited) {
  int n = grid.size();
  if (i < 0 || i >= n || j < 0 || j >= n || visited[i][j] || grid[i][j] == '0')
    return;
  visited[i][j] = 1;
  dfs(i - 1, j, grid, visited);
  dfs(i, j - 1, grid, visited);
}
void solve() {
  int n;
  cin >> n;
  int startx = -1, starty = -1;
  vector<vector<char>> grid(n, vector<char>(n));
  vector<vector<char>> visited(n, vector<char>(n));
  char x;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> x;
      grid[i][j] = x;
      ;
      visited[i][j] = 0;
    }
  }
  for (int i = 0; i < n; i++) {
    if (grid[n - 1][i] == '1' && !visited[n - 1][i])
      dfs(n - 1, i, grid, visited);
  }
  for (int i = 0; i < n; i++) {
    if (grid[i][n - 1] == '1' && !visited[i][n - 1])
      dfs(i, n - 1, grid, visited);
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (grid[i][j] == '1' && !visited[i][j]) {
        cout << "NO\n";
        return;
      }
    }
  }
  cout << "YES\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
  return 0;
}
