#include <bits/stdc++.h>
using namespace std;
vector<string> m;
bool vis[51][51];
int dfs(int i, int j) {
  vis[i][j] = true;
  int ret = 1;
  if (i > 0 and not vis[i - 1][j] and m[i - 1][j] == '1') ret += dfs(i - 1, j);
  if (j > 0 and not vis[i][j - 1] and m[i][j - 1] == '1') ret += dfs(i, j - 1);
  return ret;
}
void solve() {
  memset(vis, 0, sizeof(vis));
  m.clear();
  int n;
  cin >> n;
  int total = 0;
  for (int i = 0; i < n; i++) {
    string v;
    cin >> v;
    m.push_back(v);
    for (char c : v) {
      total += (c == '1');
    }
  }
  int cnt = 0;
  for (int j = 0; j < n; j++) {
    if (m[n - 1][j] == '1' and not vis[n - 1][j]) cnt += dfs(n - 1, j);
  }
  for (int i = 0; i < n; i++) {
    if (m[i][n - 1] == '1' and not vis[i][n - 1]) cnt += dfs(i, n - 1);
  }
  if (cnt == total)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
