1 #include<cstdio>
2 #include<cstring>
3 #define INF 2147483000
4 using namespace std;
5 int n,m,S,T;
6 struct ss{
7 int next,to,w,cp;
8 }x[5000010];
9 int first[4010],num;
10 int dep[4010];
11 int que[1000000];
12 void bui_(int ,int ,int );
13 void build(int ,int ,int );
14 int bfs();
15 int dfs(int ,int );
16 int main()
17 {
18 int TT,i,j,k,ans,add;
19 scanf("%d",&TT);
20 while(TT--){
21 memset(first,0,sizeof(first));num=0;ans=0;
22 scanf("%d",&n);
23 S=0,T=(n<<1)+1;
24 for(i=1;i<=n;i++){
25 bui_(S,i,1);bui_(n+i,T,1);
26 for(j=1;j<=n;j++){
27 scanf("%d",&k);
28 if(k)
29 bui_(i,n+j,1);
30 }
31 }
32 while(bfs())
33 while(add=dfs(S,INF))
34 ans+=add;
35 if(ans==n)printf("Yes\n");
36 else printf("No\n");
37 }
38 return 0;
39 }
40 void bui_(int f,int t,int d){
41 build(f,t,d);x[num].cp=num+1;
42 build(t,f,0);x[num].cp=num-1;
43 }
44 void build(int f,int t,int d){
45 x[++num].next=first[f];
46 x[num].to=t;
47 x[num].w=d;
48 first[f]=num;
49 }
50 int bfs(){
51 memset(dep,0,sizeof(dep));
52 int h=0,t=1,j;
53 que[t]=S;
54 while(h<t){
55 ++h;
56 for(j=first[que[h]];j;j=x[j].next)
57 if(x[j].w&&x[j].to!=S&&!dep[x[j].to]){
58 dep[x[j].to]=dep[que[h]]+1;
59 que[++t]=x[j].to;
60 }
61 }
62 if(dep[T])return 1;
63 return 0;
64 }
65 int dfs(int now,int min){
66 int j,re=0;
67 if(now==T)
68 return min;
69 for(j=first[now];j;j=x[j].next)
70 {
71 if(x[j].w&&dep[x[j].to]==dep[now]+1){
72 re=dfs(x[j].to,min<x[j].w?min:x[j].w);
73 if(re){
74 x[j].w-=re;
75 x[x[j].cp].w+=re;
76 return re;
77 }
78 dep[x[j].to]=0;
79 }
80 }
81 return re;
82 }