#include<bits/stdc++.h> using namespace std ; int a[102][102] ; int main () { int t ; scanf ( "%d" , &t ) ; while ( t-- ) { int n ; scanf ( "%d" , &n ) ; for ( int i = 0 ; i < n ; i++ ) { for ( int j = 0 ; j < n ; j++ ) { scanf ( "%d" , &a[i][j] ) ; } } int f = 0 ; for ( int i = 0 ; i < n ; i++ ) { for ( int j = 0 ; j < n ; j++ ) { if ( i-1 >= 0 && a[i-1][j] == a[i][j] ) f = 1 ; if ( i+1 < n && a[i+1][j] == a[i][j] ) f = 1 ; if ( j-1 >= 0 && a[i][j-1] == a[i][j] ) f = 1 ; if ( j+1 < n && a[i][j+1] == a[i][j] ) f = 1 ; } } if ( f ) cout << "No\n" ; else cout << "Yes\n" ; } }