#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
#define ll long long
#define pb push_back
#define fr first
#define sc second
#define MAX ((ll)(1e17+100))
#define MX ((ll)(1e6+100))
#define ARRS ((ll)(2e6+100))
#define HS ((ll)(233))
#define MOD ((ll)(1e9+7))
#define EP ((double)(1e-9))
#define LG 21
#define mul(a,b) a=((a)*(b))%MOD
using namespace std;

ll n,m;
ll a[ARRS];
ll b[ARRS];
string s;
ll pas,k,p;
vector<ll> v[ARRS];

int main(){
    ll q,n,b;
    cin>>q;
    while(q--){
        cin>>n;
        bool e=1;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(i|j){
                    cin>>k;
                    if(((i+j)&1)^b!=k)e=0;
                }
                else 
                    cin>>b;
            }
        }
        cout<<(e?"Yes":"No")<<endl;
    }





}