/*input
2
2
0 0
0 0
2
0 1
1 0
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define REP(i,j,k)     for(int i = j ; i < k ; ++i)
#define RREP(i,j,k)    for(int i = j ; i >=k ; --i)
#define A    first
#define B    second
#define mp   make_pair
#define pb   emplace_back
#define PII pair<int , int>
#define MEM(i,j)   memset(i , j , sizeof i)
#define ALL(i)     i.begin() , i.end()
#define DBGG(i,j)     cout << i << " " << j << endl
#define DB4(i,j,k,l)  cout << i << " " << j << " " << k << " " << l << endl
#define IOS cin.tie(0) , cout.sync_with_stdio(0)
#define endl "\n"
///------------------------------------------------------------
#define MAX 500
#define INF 0x3f3f3f3f

int t , n , m , x[MAX][MAX];
int32_t main(){
    IOS;
    cin >> t;
    while(t -- ){
        cin >> n , m = n;
        REP(i , 1 , n + 1) REP(j , 1 , m + 1) cin >> x[i][j];    
        int ok = 1;
        REP(i , 1 , n + 0) REP(j , 1 , m + 1) if(x[i][j] == x[i + 1][j]) ok = 0;
        REP(i , 1 , n + 1) REP(j , 1 , m + 0) if(x[i][j] == x[i][j + 1]) ok = 0;
        if(ok == 1) cout << "Yes" << endl;
        else cout << "No" << endl;
    }
    return 0;
}