#include<cstdio>
#include<iostream>
#include<stack>
#include<vector>
#include<cstring>
#define M 2016
using namespace std;
vector<int> grap[M];
stack<int> s;
char ch[M];
int low[M],num[M],vis[M],instack[M],map[M][M],f[M],n,cnt,tot;
void tarjan(int v)
{
low[v]=num[v]=++tot;
s.push(v);
instack[v]=1;
vis[v]=1;
for(int i=0;i<grap[v].size();i++){
int w=grap[v][i];
if(!vis[w]){
tarjan(w);
low[v]=min(low[v],low[w]);
}
else if(instack[w])
low[v]=min(low[v],num[w]);
}
int u;
if(low[v]==num[v]){
++cnt;
do
{
u=s.top();
f[cnt]++;
s.pop();
instack[u]=0;
}while(u!=v);
}
}
void init(){
int x;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%s",ch);
for(int j=1;j<=n;j++)
map[i][j]=(int)ch[j-1]-‘0‘;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(map[i][j]==1)grap[j].push_back(i);
for(int i=1;i<=n;i++)
if(!vis[i])tarjan(i);
if(cnt<n)printf("Yes\n");
else printf("No\n");
for(int i=1;i<=n;i++)grap[i].clear();
}
int main(){
int t;
scanf("%d",&t);
for(int i=1;i<=t;i++){
cnt=0;
memset(vis,0,sizeof(vis));
memset(low,0,sizeof(low));
memset(num,0,sizeof(num));
memset(instack,0,sizeof(instack));
memset(f,0,sizeof(f));
init();
}
return 0;
}