#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define pd(a) printf("%d\n", a);
#define plld(a) printf("%lld\n", a);
#define pc(a) printf("%c\n", a);
#define ps(a) printf("%s\n", a);
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 100010, INF = 0x7fffffff, LL_INF = 0x7fffffffffffffff;
int head[maxn], cur[maxn], d[maxn], vis[maxn];
int n, m, s, t, cnt;
struct node
{
int u, v, c, next;
}Node[maxn << 1];
void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].next = head[u];
head[u] = cnt++;
}
void add(int u, int v, int w)
{
add_(u, v, w);
add_(v, u, 0);
}
bool bfs()
{
queue<int> Q;
mem(d, 0);
Q.push(s);
d[s] = 1;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -1; i = Node[i].next)
{
node e = Node[i];
if(!d[e.v] && e.c > 0)
{
d[e.v] = d[u] + 1;
Q.push(e.v);
if(e.v == t) return 1;
}
}
}
return d[t] != 0;
}
int dfs(int u, int cap)
{
int ret = 0;
if(u == t || cap == 0)
return cap;
for(int &i = cur[u]; i != -1; i = Node[i].next)
{
node e = Node[i];
if(d[e.v] == d[e.u] + 1 && e.c > 0)
{
int V = dfs(e.v, min(cap, e.c));
Node[i].c -= V;
Node[i^1].c += V;
ret += V;
cap -= V;
if(cap == 0) break;
}
}
if(cap > 0) d[u] = -1;
return ret;
}
int Dinic(int u)
{
int ans = 0;
while(bfs())
{
// cout << 111 << endl;
memcpy(cur, head, sizeof(head));
ans += dfs(u, INF);
}
return ans;
}
void init()
{
mem(head, -1);
cnt = 0;
}
char str[maxn];
int main()
{
int T;
cin >> T;
while(T--)
{
init();
int x;
cin >> n;
s = 0, t = 2 * n + 1;
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
rd(x);
if(x == 1)
{
add(i, n + j, 1);
}
}
add(s, i, 1);
}
for(int i = 1; i <= n; i++)
add(n + i, t, 1);
if(Dinic(s) == n)
cout << "Yes" << endl;
else
cout << "No" << endl;
}
return 0;
}