#include<bits/stdc++.h> bool Mbg; using namespace std; #define vec vector #define pb push_back #define eb emplace_back #define siz(vec) (int)((vec).size()) #define all(vec) (vec).begin(),(vec).end() template<class T> void operator +=(vec<T> &a,T b){a.push_back(b);} #define pii pair<int,int> #define x first #define y second #define mp make_pair #define exc(expr) if(expr)continue; #define stop(expr) if(expr)break; #define ret(expr) if(expr)return; #define deb(var) cerr<<#var<<'='<<(var)<<"; " #define debl(var) cerr<<#var<<'='<<(var)<<";\n" #define ins insert #define era erase #define lb lower_bound #define ub upper_bound // #define int long long // template<class T> // bool Min(T &x,T y){return x>y?x=y,1:0;} // template<class T> // bool Max(T &x,T y){return x<y?x=y,1:0;} const int mod=998244353; void Add(int &x,int y){x=x+y<mod?x+y:x+y-mod;} void Dec(int &x,int y){x=x>=y?x-y:x-y+mod;} int fpm(int x,int y){ int ans=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ans=1ll*x*ans%mod;return ans; } #define sh unsigned short bool Min(sh &x,const sh &y){ return x>y?x=y,1:0; } bool Max(sh &x,const sh &y){ return x<y?x=y,1:0; } sh n; sh w[410][410],d[410]; void work(){ cin>>n; for(sh i=1;i<=n;i++){ for(sh j=1;j<=n;j++){ char ch; cin>>ch; if(i==j)w[i][j]=0; else w[i][j]=ch=='1'?1:n; } } for(sh k=1;k<=n;k++){ for(sh i=1;i<=n;i++){ for(sh j=1;j<=n;j++){ Min(w[i][j],w[i][k]+w[k][j]); } } } sh ans=n; // int TOT=0; for(sh a=1;a<=n;a++){ for(sh b=a+1;b<=n;b++){ for(sh u=1;u<=n;u++){ d[u]=min(w[u][a],w[u][b]); } sh dis=0; bool qt=0; for(sh u=1;u<=n;u++){ for(sh v=u+1;v<=n;v++){ // deb(u),debl(v); // ++TOT; if(Max(dis,min(w[u][v],(sh)(d[u]+d[v])))){ if(dis>=ans){ qt=1; break; } } } if(qt){ break; } } if(!qt){ ans=dis; } } } cout<<ans<<'\n'; // debl(TOT); } bool Med; signed main(){ ios::sync_with_stdio(0), cin.tie(0),cout.tie(0); int T=1;cin>>T;while(T--)work(); // cerr<<"Time: "<<clock()<<" ms;\n"; // cerr<<"Memory: "<<abs(&Med-&Mbg)/1024.0/1024.0<<" MiB.\n"; } /* - CONTINUE, NON-STOPPING, FOR THE FAITH - START TYPING IF YOU DON'T KNOW WHAT TO DO - STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING */
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | #include<bits/stdc++.h> bool Mbg; using namespace std; #define vec vector #define pb push_back #define eb emplace_back #define siz(vec) (int)((vec).size()) #define all(vec) (vec).begin(),(vec).end() template<class T> void operator +=(vec<T> &a,T b){a.push_back(b);} #define pii pair<int,int> #define x first #define y second #define mp make_pair #define exc(expr) if(expr)continue; #define stop(expr) if(expr)break; #define ret(expr) if(expr)return; #define deb(var) cerr<<#var<<'='<<(var)<<"; " #define debl(var) cerr<<#var<<'='<<(var)<<";\n" #define ins insert #define era erase #define lb lower_bound #define ub upper_bound // #define int long long // template<class T> // bool Min(T &x,T y){return x>y?x=y,1:0;} // template<class T> // bool Max(T &x,T y){return x<y?x=y,1:0;} const int mod=998244353; void Add(int &x,int y){x=x+y<mod?x+y:x+y-mod;} void Dec(int &x,int y){x=x>=y?x-y:x-y+mod;} int fpm(int x,int y){ int ans=1;for(;y;y>>=1,x=1ll*x*x%mod)if(y&1)ans=1ll*x*ans%mod;return ans; } #define sh unsigned short bool Min(sh &x,const sh &y){ return x>y?x=y,1:0; } bool Max(sh &x,const sh &y){ return x<y?x=y,1:0; } sh n; sh w[410][410],d[410]; void work(){ cin>>n; for(sh i=1;i<=n;i++){ for(sh j=1;j<=n;j++){ char ch; cin>>ch; if(i==j)w[i][j]=0; else w[i][j]=ch=='1'?1:n; } } for(sh k=1;k<=n;k++){ for(sh i=1;i<=n;i++){ for(sh j=1;j<=n;j++){ Min(w[i][j],w[i][k]+w[k][j]); } } } sh ans=n; // int TOT=0; for(sh a=1;a<=n;a++){ for(sh b=a+1;b<=n;b++){ for(sh u=1;u<=n;u++){ d[u]=min(w[u][a],w[u][b]); } sh dis=0; bool qt=0; for(sh u=1;u<=n;u++){ for(sh v=u+1;v<=n;v++){ // deb(u),debl(v); // ++TOT; if(Max(dis,min(w[u][v],(sh)(d[u]+d[v])))){ if(dis>=ans){ qt=1; break; } } } if(qt){ break; } } if(!qt){ ans=dis; } } } cout<<ans<<'\n'; // debl(TOT); } bool Med; signed main(){ ios::sync_with_stdio(0), cin.tie(0),cout.tie(0); int T=1;cin>>T;while(T--)work(); // cerr<<"Time: "<<clock()<<" ms;\n"; // cerr<<"Memory: "<<abs(&Med-&Mbg)/1024.0/1024.0<<" MiB.\n"; } /* - CONTINUE, NON-STOPPING, FOR THE FAITH - START TYPING IF YOU DON'T KNOW WHAT TO DO - STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING */ |