#ifdef DEBUG #include"stdlibrary.h" #else #pragma GCC target("avx") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include using namespace std; #endif // #include // #include // #include // using namespace __gnu_pbds; // #include // namespace multiprecisioninteger = boost::multiprecision; // using cint=multiprecisioninteger::cpp_int; using ll=long long; using datas=pair; using ddatas=pair; using tdata=pair; using vec=vector; using mat=vector; using pvec=vector; using pmat=vector; // using llset=tree,rb_tree_tag,tree_order_statistics_node_update>; #define For(i,a,b) for(i=a;i<(ll)b;++i) #define bFor(i,b,a) for(i=b,--i;i>=(ll)a;--i) #define rep(i,N) For(i,0,N) #define rep1(i,N) For(i,1,N) #define brep(i,N) bFor(i,N,0) #define brep1(i,N) bFor(i,N,1) #define all(v) (v).begin(),(v).end() #define allr(v) (v).rbegin(),(v).rend() #define vsort(v) sort(all(v)) #define vrsort(v) sort(allr(v)) #define uniq(v) vsort(v),(v).erase(unique(all(v)),(v).end()) #define endl "\n" #define popcount __builtin_popcountll #define eb emplace_back #define print(x) cout< ostream& operator<<(ostream& os,const pair& p){return os<<"("< ostream& operator<<(ostream& os,const vector& v){ os<<"{";bool f=false; for(auto& x:v){if(f)os<<",";os< ostream& operator<<(ostream& os,const set& v){ os<<"{";bool f=false; for(auto& x:v){if(f)os<<",";os< ostream& operator<<(ostream& os,const multiset& v){ os<<"{";bool f=false; for(auto& x:v){if(f)os<<",";os< ostream& operator<<(ostream& os,const map& v){ os<<"{";bool f=false; for(auto& x:v){if(f)os<<",";os< inline bool chmax(T& a,const T b){bool x=a inline bool chmin(T& a,const T b){bool x=a>b;if(x)a=b;return x;} #ifdef DEBUG void debugg(){cout<void debugg(const T& x,const Args&... args){cout<<" "<sync_with_stdio(0); int T; cin>>T; while(T--){ int i,j,N,P; cin>>N>>P; if(P!=2){ rep(i,N)rep(j,N)cin>>s; print("0"); continue; } vector cntx(N),cnty(N); vector> g(N,vector(N)); rep(i,N)rep(j,N){ cin>>g[i][j]; if(g[i][j]==-1){ cntx[i]++; cnty[j]++; } } if(max(*max_element(all(cntx)),*max_element(all(cnty)))>1){ print("0"); continue; } vector chx,chy; rep(i,N)if(!cntx[i])chx.emplace_back(i); rep(i,N)if(!cnty[i])chy.emplace_back(i); assert(chx.size()==chy.size()); K=chx.size(); if(!K){ print("1"); continue; } vector> bs(K); rep(i,chx.size())rep(j,chy.size())if(g[chx[i]][chy[j]])bs[i][j].flip(); rep(i,K){ if(!bs[i][i]){ For(j,i+1,K)if(bs[j][i]){ swap(bs[i],bs[j]); break; } if(j==K)break; } For(j,i+1,K)if(bs[j][i])bs[j]^=bs[i]; } print(int(i==K)); } }