#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;
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgdG90YWxfdGVzdDsKCmludCBtYWluKCkKewogICAgaW9zOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKCiAgICBjaW4+PnRvdGFsX3Rlc3Q7CiAgICB3aGlsZSh0b3RhbF90ZXN0LS0pCiAgICB7CiAgICAgICAgaW50IG47CiAgICAgICAgY2luPj5uOwogICAgICAgIGludCBhW24rNV1bbis1XTsKICAgICAgICBpbnQgY29sW24rNV07CiAgICAgICAgaW50IHJvd1tuKzVdOwogICAgICAgIGludCBkMVsyKm4gKyA1XSwgZDJbMipuICsgNV07CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICAgICAgewogICAgICAgICAgICByb3dbaV0gPSAwOwogICAgICAgICAgICBmb3IoaW50IGogPSAxOyBqIDw9IG47IGorKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY29sW2pdID0gMDsKICAgICAgICAgICAgICAgIGQxW24gLSBpICsgal0gPSAwOwogICAgICAgICAgICAgICAgZDJbbiAtIGkgKyBqXSA9IDA7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZm9yKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICAgICAgewogICAgICAgICAgICBmb3IoaW50IGogPSAxOyBqIDw9IG47IGorKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgY2luPj5hW2ldW2pdOwogICAgICAgICAgICAgICAgcm93W2ldICs9IGFbaV1bal07CiAgICAgICAgICAgICAgICBjb2xbal0gKz0gYVtpXVtqXTsKICAgICAgICAgICAgICAgIGQxW24gLSBpICsgal0gKz0gYVtpXVtqXTsKICAgICAgICAgICAgICAgIGQyW24gKyBpIC0gal0gKz0gYVtpXVtqXTsKICAgICAgICAgICAgfQogICAgICAgIH0KCiAgICAgICAgYm9vbCBjaGVjayA9IHRydWU7CiAgICAgICAgLy9yb3csIGNvbAogICAgICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgaWYocm93W2ldID4gMSB8fCBjb2xbaV0gPiAxKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBjaGVjayA9IGZhbHNlOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIC8vZGlhZ29uYWwKICAgICAgICBmb3IoaW50IGkgPSAxOyBpIDw9IDIqbiAtIDE7IGkrKykKICAgICAgICB7CiAgICAgICAgICAgIGlmKGQxW2ldID4gMSB8fCBkMltpXSA+IDEpCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGNoZWNrID0gZmFsc2U7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgY291dDw8Y2hlY2s8PGVuZGw7CiAgICB9Cn0K