// In the name of God.
// You're anything and We're nothing.
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e2 + 17;
int t;
bool a[maxn][maxn];
int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> t;
    while(t--){
        int n;
        cin >> n;
        bool ok = 1;
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++){
                cin >> a[i][j];
                if(i)
                    ok &= (a[i][j] ^ a[i - 1][j]) == 1;
                if(j)
                    ok &= (a[i][j] ^ a[i][j - 1]) == 1;
            }
        cout << (ok ? "Yes" : "No") << '\n';
    }
}