#include <bits/stdc++.h>
using namespace std;

int total_test;

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);

    cin>>total_test;
    while(total_test--)
    {
        int n;
        cin>>n;
        int a[n+5][n+5];
        int col[n+5];
        int row[n+5];
        int d1[2*n + 5], d2[2*n + 5];
        for(int i = 1; i <= n; i++)
        {
            row[i] = 0;
            for(int j = 1; j <= n; j++)
            {
                col[j] = 0;
                d1[n - i + j] = 0;
                d2[n - i + j] = 0;
            }
        }
        for(int i = 1; i <= n; i++)
        {
            for(int j = 1; j <= n; j++)
            {
                cin>>a[i][j];
                row[i] += a[i][j];
                col[j] += a[i][j];
                d1[n - i + j] += a[i][j];
                d2[n + i - j] += a[i][j];
            }
        }

        bool check = true;
        //row, col
        for(int i = 1; i <= n; i++)
        {
            if(row[i] > 1 || col[i] > 1)
            {
                check = false;
            }
        }
        //diagonal
        for(int i = 1; i <= 2*n - 1; i++)
        {
            if(d1[i] > 1 || d2[i] > 1)
            {
                check = false;
            }
        }
        cout<<check<<endl;
    }
}
