/*     !ya khoda!     */
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define ld long double

#define pp push
#define po pop()
#define pb push_back
#define pf push_front
#define pob pop_back()
#define pof pop_front()
#define I insert
#define E erase

#define F first
#define S second

#define mkp make_pair
#define pll pair<ll,ll>
#define smax(a,b) a=max(a,b)
#define smin(a,b) a=min(a,b)
#define all(a) a.begin(),a.end()

#define coutghat(x)      cout<<tolower(x);
#define coutashar(x,d)   cout<<fixed<<setprecision(x)<<(ld)d
#define IOS ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0);

#define LB(x) ((x) & -(x))
#define BIT(a , b) (((a)>>(b)) & 1)
//#define poow(a,b,M)   b?(poow((a*a)%M,b/2)%M * (b&1?a))%M:1 ;
/*==========================================================================*/
using namespace std;
using namespace __gnu_pbds;
template <typename T> using ordered_set =  tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T> using ordered_multiset =  tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
ll poow(ll x,ll n,ll Mod=1e9+7) { return (n==0?1:poow(x*x%Mod,n/2)*(n&1?x:1)%Mod); }
//#pragma optimize O3
// 107,109, 211,223, 307, 401,409, 503,509, 601,607, 701,709, 809, 907, 1009
/*==========================================================================*/
const int N=2e3+100 , M=1e3+100 , LG=23 , base=2;
const int MOD=1e9+7 , mod=1e9+9;
const ll  INF=1e9   , inf=1e18 , super_inf=~0ull/4;
ll n,a[N][N] , cnt;
vector<ll>g[N][5];
bool vis[N][5];
/*==========================================================================*/
void dfs(ll v,ll p)
{   vis[v][p]=1;
    for(int i=0;i<g[v][p].size();i++)
        if(!vis[g[v][p][i]][p])
            dfs(g[v][p][i],p);
}
/*==========================================================================*/
int main()
{
    IOS;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {   cin>>a[i][j];
            if(a[i][j]>0)
                g[i][0].pb(j) , g[j][1].pb(i);
        }
    }

    dfs(0,0) , dfs(0,1);
    for(int i=0;i<n;i++)
        if(!vis[i][0] || !vis[i][1])
            return cout<<"NO" , 0;
    cout<<"YES";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define eps 1e-9
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define MXN 2005

int a[MXN][MXN], n;
bool vis[MXN][MXN];

void dfs(int x, int id, int pa){
	if(vis[id][x]) return;
	if(pa) vis[id][x] = 1;
	for(int i = 0; i < n; i ++){
		if(a[x][i]) dfs(i, id, pa + 1);
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++)
			scanf("%d", &a[i][j]);
	for(int i = 0; i < n; i ++) dfs(i, i, 0);
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++){
			if(!vis[i][j]){
				puts("NO");
				return 0;
			}
		}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define eps 1e-9
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define MXN 2005

int a[MXN][MXN], n;
bool vis[MXN][MXN];

void dfs(int x, int id, int pa){
	if(pa) vis[id][x] = 1;
	for(int i = 0; i < n; i ++){
		if(a[x][i] && !vis[id][i]) dfs(i, id, pa + 1);
	}
}

int main(){
	scanf("%d", &n);
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++)
			scanf("%d", &a[i][j]);
	for(int i = 0; i < n; i ++) dfs(i, i, 0);
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++){
			if(!vis[i][j]){
				puts("NO");
				return 0;
			}
		}
	puts("YES");
}


#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define PB push_back
#define F first
#define S second
#define MP make_pair
#define eps 1e-9
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define MXN 1005

vector<PII> v[105];
vector<int> tp;
int ans, n, s;

int fx(int i, int t){
	if(t >= v[i][v[i].size() - 1].F) return s;
	int pos = lower_bound(v[i].begin(), v[i].end(), MP(t, -1)) - v[i].begin();
	int dy = (v[i][pos].S - v[i][pos - 1].S) / (v[i][pos].F - v[i][pos - 1].F);
	int dt = t - v[i][pos - 1].F;
	return v[i][pos - 1].S + dy * dt;
}

int main(){
	int ki, vi, t, cur, nt;
	cin >> n >> s;
	for(int i = 0; i < n; i ++){
		scanf("%d", &ki);
		v[i].PB(MP(0, 0));
		cur = nt = 0;
		while(ki --){
			scanf("%d %d", &vi, &t);
			nt += t;
			cur += t * vi;
			v[i].PB(MP(nt, cur));
		}
	}
	for(int i = 0; i < n; i ++){
		for(int j = i + 1; j < n; j ++){
			LL pi = 0, pj = 0, ni, nj;
			tp.clear();
			for(auto k : v[i]) tp.PB(k.F);
			for(auto k : v[j]) tp.PB(k.F);
			sort(tp.begin(), tp.end());
			tp.resize(unique(tp.begin(), tp.end()) - tp.begin());
			for(auto k : tp){
				if(k == 0) continue;
				ni = fx(i, k);
				nj = fx(j, k);
				if(pi == pj && pi == 0){
					pi = ni;
					pj = nj;
					continue;
				}
				if((ni - nj) * (pi - pj) < 0){
					ans ++;
					pi = ni;
					pj = nj;
				}
			}
		}
	}
	printf("%d\n", ans);
}


#include <bits/stdc++.h>
using namespace std;
typedef double D;
typedef long double LD;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
#define PB push_back
#define FZ(x) memset(x, 0 ,sizeof(x))
#define F first
#define S second
#define MP make_pair
#define eps 1e-9
inline bool equal( D _x ,  D _y ){
  return _x > _y - eps && _x < _y + eps;
}
#define MXN 2005

int a[MXN][MXN];
bool vis[MXN][MXN];

struct Scc{
	int n, nScc, vst[MXN], bln[MXN];
	vector<int> E[MXN], rE[MXN], vec;
	void init(int _n){
		n = _n;
		for (int i=0; i<MXN; i++){
			E[i].clear();
			rE[i].clear();
		}
	}
	void add_edge(int u, int v){
		E[u].PB(v);
		rE[v].PB(u);
	}
	void DFS(int u){
		vst[u]=1;
		for (auto v : E[u])
			if (!vst[v]) DFS(v);
		vec.PB(u);
	}
	void rDFS(int u){
		vst[u] = 1;
		bln[u] = nScc;
		for (auto v : rE[u])
		if (!vst[v]) rDFS(v);
	}
	void solve(){
		nScc = 0;
		vec.clear();
		FZ(vst);
		for (int i=1; i<=n; i++)
			if (!vst[i]) DFS(i);
		reverse(vec.begin(),vec.end());
		FZ(vst);
		for (auto v : vec){
			if (!vst[v]){
				rDFS(v);
				nScc++;
			}
		}
	}
}graph;

int main(){
	int n, t;
	scanf("%d", &n);
	graph.init(n);
	for(int i = 1; i <= n; i ++)
		for(int j = 1; j <= n; j ++){
			scanf("%d", &t);
			if(t) graph.add_edge(i, j);
		}
	graph.solve();
	if(graph.nScc > 1) puts("NO");
	else puts("YES");
}


//good question+approach matters
//converted matrix to adjacency graph
//and then two times dfs
/*Learnt--if a ways matrix(i,j) given telling the no of ways to reach from i to j
(using 1 edge) then we can find out the no of ways using k ways by finding the
matrix a^k. */

//written by Aman Mishra(aspro)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,s,e) for(int i=s;i<=e;i++)
#define aspro ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define pii pair<int,int>
#define INF 10000000000000
#define MOD 1000000007
#define test int t;cin>>t;while(t--)
bool arr[2005][2005],visit[2005];
int mark[2005],n;
stack<int> s;
void dfs1(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs1\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[x][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
}
void dfs2(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs2\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[x][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
}
int main()
{
    aspro
    int a;
    cin>>n;
    memset(mark,0,sizeof mark);
    f(i,1,n) f(j,1,n)
    {
        cin>>a;
        if(a) arr[i][j]=1;
        else  arr[i][j]=0;
    }
    f(i,1,n)
    {
        if(arr[i][i])
        {
            dfs1(1);
            dfs2(1);
        }
    }

    f(i,1,n)
    {
        if(mark[i]!=2)
        {
            cout<<"NO\n";
            return 0;
        }
    }
    cout<<"YES\n";
    return 0;

}


//good question+approach matters
//converted matrix to adjacency graph
//and then two times dfs
/*Learnt--if a ways matrix(i,j) given telling the no of ways to reach from i to j
(using 1 edge) then we can find out the no of ways using k ways by finding the
matrix a^k. */

//written by Aman Mishra(aspro)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,s,e) for(int i=s;i<=e;i++)
#define aspro ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define pii pair<int,int>
#define INF 10000000000000
#define MOD 1000000007
#define test int t;cin>>t;while(t--)
bool arr[2005][2005],visit[2005];
int mark[2005],n;
stack<int> s;
void dfs1(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs1\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[x][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
}
void dfs2(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs2\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[i][x]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
}
int main()
{
    aspro
    int a;
    cin>>n;
    memset(mark,0,sizeof mark);
    f(i,1,n) f(j,1,n)
    {
        cin>>a;
        if(a) arr[i][j]=1;
        else  arr[i][j]=0;
    }
    f(i,1,n)
    {
        if(arr[i][i])
        {
            dfs1(i);
            dfs2(i);
            break;
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
    f(i,1,n)
    {
        if(mark[i]!=2)
        {
            cout<<"NO\n";
            return 0;
        }
    }
    cout<<"YES\n";
    return 0;

}


//good question+approach matters
//converted matrix to adjacency graph
//and then two times dfs
/*Learnt--if a ways matrix(i,j) given telling the no of ways to reach from i to j
(using 1 edge) then we can find out the no of ways using k ways by finding the
matrix a^k. */

//written by Aman Mishra(aspro)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,s,e) for(int i=s;i<=e;i++)
#define aspro ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define pii pair<int,int>
#define INF 10000000000000
#define MOD 1000000007
#define test int t;cin>>t;while(t--)
bool arr[2005][2005],visit[2005];
int mark[2005],n;
stack<int> s;
void dfs1(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs1\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[f][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
}
void dfs2(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs2\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[i][f]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
}
int main()
{
    aspro
    int a;
    cin>>n;
    memset(mark,0,sizeof mark);
    f(i,1,n) f(j,1,n)
    {
        cin>>a;
        if(a) arr[i][j]=1;
        else  arr[i][j]=0;
    }
    f(i,1,n)
    {
        if(arr[i][i])
        {
            dfs1(i);
            dfs2(i);
            break;
        }
    }
    //f(i,1,n) cout<<mark[i]<<" ";cout<<endl;
    f(i,1,n)
    {
        if(mark[i]!=2)
        {
            cout<<"NO\n";
            return 0;
        }
    }
    cout<<"YES\n";
    return 0;

}


//good question+approach matters
//converted matrix to a graph
//and then floyd warshall

//written by Aman Mishra(aspro)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,s,e) for(ll i=s;i<=e;i++)
#define aspro ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define pii pair<int,int>
#define INF 10000000000000
#define MOD 1000000007
#define test int t;cin>>t;while(t--)
ll dist[55][55];
bool arr[55][55];
int main()
{
    aspro
    ll n,a;
    cin>>n;
    f(i,1,n) f(j,1,n) dist[i][j]=INF;
    f(i,1,n) f(j,1,n)
    {
        cin>>a;
        if(a)
        {
            arr[i][j]=1;
            dist[i][j]=1;
        }
        else  arr[i][j]=0;
    }
    f(k,1,n) f(i,1,n) f(j,1,n)
        if(dist[i][k]+dist[k][j]<dist[i][j])
            dist[i][j]=dist[i][k]+dist[k][j];
    f(i,1,n) dist[i][i]=1;
    f(i,1,n)
    {
        bool f1=1,f2=1;
        f(j,1,n)
        {
            if(dist[i][j]==INF)
            {
                f1=0;
                break;
            }
        }
        f(j,1,n)
        {
            if(dist[j][i]==INF)
            {
                f1=0;
                break;
            }
        }
        if(f1&&f2)
        {
            cout<<"YES\n";
            return 0;
        }
    }
    cout<<"NO\n";
    return 0;

}


//good question+approach matters
//converted matrix to a graph
//and then floyd warshall

//written by Aman Mishra(aspro)
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i,s,e) for(int i=s;i<=e;i++)
#define aspro ios_base::sync_with_stdio(0);cin.tie(0);
#define pb push_back
#define pii pair<int,int>
#define INF 10000000000000
#define MOD 1000000007
#define test int t;cin>>t;while(t--)
bool arr[2005][2005],visit[2005];
int mark[2005],n;
stack<int> s;
void dfs1(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs1\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[x][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
}
void dfs2(int x)
{
    memset(visit,0,sizeof visit);
    while(!s.empty()) s.pop();
    s.push(x);
    visit[x]=1;mark[x]++;
    while(!s.empty())
    {//cout<<"in dfs2\n";
        int f=s.top();
        s.pop();
        f(i,1,n)
        {
            if(arr[x][i]&&!visit[i])
            {
                visit[i]=1;
                mark[i]++;
                s.push(i);
            }
        }
    }
}
int main()
{
    aspro
    int a;
    cin>>n;
    memset(mark,0,sizeof mark);
    f(i,1,n) f(j,1,n)
    {
        cin>>a;
        if(a) arr[i][j]=1;
        else  arr[i][j]=0;
    }
    dfs1(1);
    dfs2(1);
    f(i,1,n)
    {
        if(mark[i]!=2)
        {
            cout<<"NO\n";
            return 0;
        }
    }
    cout<<"YES\n";
    return 0;

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int a[2000][2000];

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> a[i][j];
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (a[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (a[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int b[2000][2000];
int used[2000];
int start;
vector<int> g[2000];
vector<int> gg[2000];
void dfs(int u)
{
	a[start][u] = 1;
	FOR(i,0,g[u].size())
	{
		int v = g[u][i];
		if (c[u][v] == 0)
			continue;
		if (!a[start][v])
		{
			dfs(v);
		}
	}
}

void dfs2(int u)
{
	b[start][u] = 1;
	FOR(i,0,gg[u].size())
	{
		int v = gg[u][i];
		if (c[u][v] == 0)
			continue;
		if (!b[start][v])
		{
			dfs2(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
			c[i][j] = 0;
			if (c[i][j])
			{
				g[i].pb(j);
				gg[j].pb(i);
			}
		}
	}
	FOR(i,0,n)
	{
		if (c[i][i] == 0)
			continue;
		memset(used,0,sizeof used);
		start = i;
		dfs(i);
		memset(used,0,sizeof used);
		start = i;
		dfs2(i);
		break;
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (i != start)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
		flag = true;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int b[2000][2000];
int used[2000];
int start;
vector<int> g[2000];
vector<int> gg[2000];
void dfs(int u)
{
	a[start][u] = 1;
	FOR(i,0,g[u].size())
	{
		int v = g[u][i];
		if (!a[start][v])
		{
			dfs(v);
		}
	}
}

void dfs2(int u)
{
	b[start][u] = 1;
	FOR(i,0,gg[u].size())
	{
		int v = gg[u][i];
		if (!b[start][v])
		{
			dfs2(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
			if (c[i][j])
			{
				g[i].pb(j);
				gg[j].pb(i);
			}
		}
	}
	FOR(i,0,n)
	{
		if (c[i][i] == 0)
			continue;
		start = i;
		dfs(i);
		dfs2(i);
		break;
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (i != start)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		FOR(j,0,n)
		{
			if (b[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int used[2000];
int start;
void dfs(int u)
{
	a[start][u] = 1;
	FOR(i,0,n)
	{
		if (c[u][i] == 0)
			continue;
		int v = i;
		if (!a[start][v])
		{
			dfs(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
		}
	}
	FOR(i,0,n)
	{
		memset(used,0,sizeof used);
		start = i;
		dfs(i);
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int b[2000][2000];
int used[2000];
int start;
vector<int> g[2000];
vector<int> gg[2000];
void dfs(int u)
{
	a[start][u] = 1;
	FOR(i,0,g[u].size())
	{
		int v = g[u][i];
		if (c[u][v] == 0)
			continue;
		if (!a[start][v])
		{
			dfs(v);
		}
	}
}

void dfs2(int u)
{
	b[start][u] = 1;
	FOR(i,0,gg[u].size())
	{
		int v = gg[u][i];
		if (c[u][v] == 0)
			continue;
		if (!b[start][v])
		{
			dfs2(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
			if (c[i][j])
			{
				g[i].pb(j);
				gg[j].pb(i);
			}
		}
	}
	FOR(i,0,n)
	{
		if (c[i][i] == 0)
			continue;
		memset(used,0,sizeof used);
		start = i;
		dfs(i);
		memset(used,0,sizeof used);
		start = i;
		dfs2(i);
		break;
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (i != start)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
		flag = true;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int used[2000];
int start;
vector<int> g[2000];
void dfs(int u)
{
	a[start][u] = 1;
	FOR(i,0,g[u].size())
	{
		int v = g[u][i];
		if (c[u][v] == 0)
			continue;
		if (!a[start][v])
		{
			dfs(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
			if (c[i][j])
				g[i].pb(j);
		}
	}
	FOR(i,0,n)
	{
		memset(used,0,sizeof used);
		start = i;
		dfs(i);
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <istream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
//#include <unordered_map>
#include <cstring>
#include <string.h>
#include <iomanip>
#include <queue>
#include <stack>
#include <list>
#include <sstream>
//#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld;

#define ALL(V) V.begin(), V.end()
#define SZ(V) (int)V.size()
#define pb push_back
#define mp make_pair
#define f first
#define s second

#define fopen freopen("substrcmp.in", "r", stdin); freopen("substrcmp.out","w", stdout)
#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define FORR(i, b, a) for(int i = (b) - 1; i >= (a); --i)
#define FILL(A,value) memset(A,value,sizeof(A))
#define IOSFAST ios::sync_with_stdio(false); cin.tie(0)

const ll MOD = 1000LL * 1000 * 1000 + 7;
const int INF = 1e9;
const ll base = 1e18;
const int MAX = 1000;
const double EPS = 1e-9;
const double PI = acos(-1.);

int n;
int c[2000][2000];
int a[2000][2000];
int used[2000];
void dfs(int u)
{
	used[u] = 1;
	FOR(i,0,n)
	{
		if (c[u][i] == 0)
			continue;
		int v = i;
		if (!used[v])
		{
			dfs(v);
		}
	}
}

int main()
{
	IOSFAST;
	cin >> n;
	FOR(i,0,n)
	{
		FOR(j,0,n)
		{
			cin >> c[i][j];
		}
	}
	FOR(i,0,n)
	{
		memset(used,0,sizeof used);
		dfs(i);
		FOR(j,0,n)
		{
			a[i][j] = used[j];
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[i][j]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}
	FOR(i,0,n)
	{
		bool flag = true;
		if (c[i][i] == 0)
			continue;
		FOR(j,0,n)
		{
			if (a[j][i]==0)
			{
				flag = false;
				break;
			}
		}
		if (flag)
		{
			cout << "YES";
			return 0;
		}
	}

	cout << "NO";

}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <exception>
#include <numeric>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <deque>
using namespace std;

typedef long long lo;
typedef vector< vector<lo> > vvl;
typedef vector< lo > vl;
typedef pair<lo, lo> ll;
typedef vector< pair<lo, lo> > vll;
typedef vector< vll > vvll;
typedef pair<lo, pair<lo, lo> > lll;
typedef pair<lo, lll > llll;
typedef vector<lll> vlll;
typedef vector<vvl> vvvl;
typedef vector<set<lo> > vs;
typedef map<lo, map<lo, lo> > mm;

#define N 100100
#define M 1001000
#define LOGN 20

vl gr[N][2];
lo vis[N], cnt = 0;

lo dfs(lo v, lo c){
    vis[v]=c;
    cnt++ ;
    for (lo nb:gr[v][c-1])
        if (vis[nb]!=c) dfs(nb, c);

}

int main() {
	// ios_base::sync_with_stdio(false);
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);

    lo n,a;
    cin >> n ;

    for (lo i=0;i<n;i++)
        for (lo j=0;j<n;j++){
            cin >> a;
            if (a) gr[i][0].push_back(j), gr[j][1].push_back(i);
        }

    dfs(0, 1); dfs(0, 2);

    cout << (cnt == 2*n ? "YES" : "NO");

	return 0;
}


#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <exception>
#include <numeric>
#include <algorithm>
#include <sstream>
#include <cstring>
#include <deque>
using namespace std;

typedef long long lo;
typedef vector< vector<lo> > vvl;
typedef vector< lo > vl;
typedef pair<lo, lo> ll;
typedef vector< pair<lo, lo> > vll;
typedef vector< vll > vvll;
typedef pair<lo, pair<lo, lo> > lll;
typedef pair<lo, lll > llll;
typedef vector<lll> vlll;
typedef vector<vvl> vvvl;
typedef vector<set<lo> > vs;
typedef map<lo, map<lo, lo> > mm;

#define N 100100
#define M 1001000
#define LOGN 20

vl gr[N][2];
lo vis[N], cnt = 0;

lo dfs(lo v, lo c){
    vis[v]=c;
    cnt++ ;
    for (lo nb:gr[v][c-1])
        if (vis[nb]!=c) dfs(nb, c);

}

int main() {
	ios_base::sync_with_stdio(false);
	// freopen("input.txt","r",stdin);
	// freopen("output.txt","w",stdout);

    lo n,a;
    cin >> n ;

    for (lo i=0;i<n;i++)
        for (lo j=0;j<n;j++){
            cin >> a;
            if (a) gr[i][0].push_back(j), gr[j][1].push_back(i);
        }

    dfs(0, 1); dfs(0, 2);

    cout << (cnt == 2*n ? "YES" : "NO");

	return 0;
}


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

const int maxn = 2000 + 10;
int a[maxn][maxn];
vector<int> G[maxn], R[maxn], topo;
bool v[maxn];

void rdfs(int now) {
    v[now] = true;
    for (int u : R[now]) if (!v[u]) rdfs(u);
    topo.push_back(now);
}

void dfs(int now) {
    v[now] = true;
    for (int u : G[now]) if (!v[u]) dfs(u);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) {
        cin >> a[i][j];
        if (a[i][j]) G[i].push_back(j), R[j].push_back(i);
    }
    for (int i = 1; i <= n; ++i) if (!v[i]) rdfs(i);
    reverse(topo.begin(), topo.end());
    memset(v, false, sizeof(v));
    dfs(topo[0]);
    for (int i = 1; i <= n; ++i) if (!v[i]) return cout << "NO" << endl, 0;
    cout << "YES" << endl;
    return 0;
}


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

const int maxn = 2000 + 10;
int a[maxn][maxn];
vector<int> G[maxn];
bool v[maxn];

void dfs(int now) {
    v[now] = true;
    for (int u : G[now]) if (!v[u]) dfs(u);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) {
        cin >> a[i][j];
        if (a[i][j]) G[i].push_back(j);
    }
    int cc = 0;
    for (int i = 1; i <= n; ++i) if (!v[i]) ++cc, dfs(i);
    if (cc > 1) cout << "NO" << endl;
    else cout << "YES" << endl;
    return 0;
}


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

const int maxn = 2000 + 10;
int a[maxn][maxn];
vector<int> G[maxn], R[maxn], topo;
bool v[maxn];

void rdfs(int now) {
    v[now] = true;
    for (int u : G[now]) if (!v[u]) rdfs(u);
    topo.push_back(now);
}

void dfs(int now) {
    v[now] = true;
    for (int u : G[now]) if (!v[u]) dfs(u);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) {
        cin >> a[i][j];
        if (a[i][j]) G[i].push_back(j), R[j].push_back(i);
    }
    rdfs(1);
    reverse(topo.begin(), topo.end());
    memset(v, false, sizeof(v));
    dfs(topo[0]);
    for (int i = 1; i <= n; ++i) if (!v[i]) return cout << "NO" << endl, 0;
    cout << "YES" << endl;
    return 0;
}


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

const int maxn = 2000 + 10;
int a[maxn][maxn];
vector<int> G[maxn];
bool v[maxn];

void dfs(int now) {
    v[now] = true;
    for (int u : G[now]) if (!v[u]) dfs(u);
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) {
        cin >> a[i][j];
        if (a[i][j]) G[i].push_back(j);
    }
    for (int i = 1; i <= n; ++i) {
        memset(v, false, sizeof(v));
        dfs(i);
        for (int j = 1; j <= n; ++j) if (!v[j]) return cout << "NO" << endl, 0;
    }
    cout << "YES" << endl;
    return 0;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		cnt++ ;
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			int x ;
			cin >> x ;
			if(x)
			    adj[i].pb(j);
		}
	}
	for(int i=0; i<n; i++)
	    if(!mark[i])
        	DFS(i) ;
	if(cnt==1)
		cout << "YES\n" ;
	else
		cout << "NO\n" ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		cnt++ ;
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			int x ;
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	if(cnt==1)
		cout << "YES\n" ;
	else
		cout << "NO\n" ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN];
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	int cnt=0 ;
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			cnt++ ;
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		if(cnt<n)
			cout << "YES" ;
		else
			cout << "NO" ;
		exit(0);
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
		    int x ;
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			cnt++ ;
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		cnt++ ;
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			int x ;
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	if(cnt==1)
		cout << "YES\n" ;
	else
		cout << "NO\n" ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN];
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	int cnt=0 ;
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			cnt++ ;
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		if(cnt<n)
			cout << "NO" ;
		else
			cout << "YES" ;
		exit(0);
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
		    int x ;
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN];
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			cnt++ ;
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		cnt++ ;
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			int x ;
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	if(cnt==1)
		cout << "YES\n" ;
	else
		cout << "NO\n" ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], m, st[maxN], start, high[maxN], on[maxN], cnt;
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		cnt++ ;
	}
}

int main()
{
    fast ;
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			int x ;
			cin >> x ;
			if(x)
			    adj[i].pb(j);
		}
	}
	for(int i=0; i<n; i++)
	    if(!mark[i])
        	DFS(i) ;
	if(cnt==1)
		cout << "YES\n" ;
	else
		cout << "NO\n" ;
	return 0 ;
}


#include<iostream>
#include<algorithm>
#include<string>
#include<stack>
#include<iomanip>
#include<vector>
#include<map>
using namespace std;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
typedef long long ll;
typedef pair<ll,ll> pii;

const int maxN = 2000 + 5 ;
int n, mark[maxN], ind , cnt, m;
vector<int> adj[maxN] ;
vector<int> s ;

void DFS(int v)
{
	s.pb(v) ;
	on[v] = mark[v] = 1 ;
	st[v] = high[v] = start++ ;
	for(int i=0; i<adj[v].size(); i++)
	{
		int x = adj[v][i];
		if(mark[x])
		{
			if(on[x])
				high[v] = min(high[v], high[x]) ;
		}
		else
		{
			DFS(x) ;
			high[v] = min(high[v], high[x]) ;
		}
	}
	int cnt=0 ;
	if(st[v]==high[v])
	{
		int x = s.back();
		while(!s.empty() && st[x]>=st[v])
		{
			cnt++ ;
			on[x] = 0 ;
			s.pop_back() ;
			x = s.back() ;

		}
		if(cnt<n)
			cout << "NO" ;
		else
			cout << "YES" ;
		exit(0);
	}
}

int main()
{
	cin >> n ;
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			cin >> x ;
			adj[i].pb(j);
		}
	}
	DFS(0) ;
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef deque<int> qi;
#define pb push_back
#define fori(i,s,n) for(int i = s; i < n; i++)
#define zero(a) memset(a, 0, sizeof(a))

const ll LOG = 20, N = 2010;

int a[N][N], n, cnt;
bool vis[2][N];
void dfs(int k, int r)
{
	vis[k][r] = true, cnt++;
	fori(i, 0, n)
		if (!vis[k][i] && ((k && a[r][i]) || (!k && a[i][r])))
			dfs(k, i);
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	fori(i, 0, n)
		fori(j, 0, n)
		cin >> a[i][j];
	dfs(0, 1);
	//dfs(1, 1);
	cout << (cnt == n ? "YES" : "NO");
	cin >> n;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef deque<int> qi;
#define pb push_back
#define fori(i,s,n) for(int i = s; i < n; i++)
#define zero(a) memset(a, 0, sizeof(a))

const ll LOG = 20, N = 2010;

int a[N][N], n, cnt;
bool vis[2][N];
void dfs(int k, int r)
{
	vis[k][r] = true, cnt++;
	fori(i, 0, n)
		if (!vis[k][i] && ((k && a[r][i]) || (!k && a[i][r])))
			dfs(k, i);
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	fori(i, 0, n)
		fori(j, 0, n)
		cin >> a[i][j];
	dfs(0, 1);
	dfs(1, 1);
	cout << (cnt == n * 2 ? "YES" : "NO");
	cin >> n;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> piii;
typedef deque<int> qi;
#define pb push_back
#define fori(i,s,n) for(int i = s; i < n; i++)
#define zero(a) memset(a, 0, sizeof(a))

const ll LOG = 20, N = 2010;

int a[N][N], n, cnt;
bool vis[2][N];
void dfs(int k, int r)
{
	vis[k][r] = true, cnt++;
	fori(i, 0, n)
		if (!vis[k][i] && ((k && a[r][i]) || (!k && a[i][r])))
			dfs(k, i);
}
int main()
{
	cin >> n;
	fori(i, 0, n)
		fori(j, 0, n)
		cin >> a[i][j];
	dfs(0, 1);
	dfs(1, 1);
	cout << (cnt == n * 2 ? "YES" : "NO");
	cin >> n;
}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=0;
void dfs(int s,int lev)
{

    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            dfs(i,lev+1);
        }
        else
            {
                if(v[s][i] && vis[i] && lev==n-1 && i==0)
                {
                    //trace4(i,s,lev,v[s][i]);
                    ch=1;
                }
            }
    }
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }
    vis.resize(n,0);
    vis[0]=1;
    dfs(0,0);
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{

    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            dfs(i,lev+1);
            cnt++;
        }

    }
    vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<n;i++)
    {

        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            ch=0;
            break;
        }

    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<n;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);
#include <bits/stdc++.h>
using namespace std;

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<n;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            trace1(i);
            ch=0;
            break;
        }

    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}
        if(cnt!=n-1)
        {
            trace1(i);
            ch=0;
            break;
        }

    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{

    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            dfs(i,lev+1);
            cnt++;
        }

    }
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }

    for(int i=0;i<n;i++)
    {
        vis.clear();
        vis.resize(n,0);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            ch=0;
            break;
        }

    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    vi deg(n,0);
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
            if(v[i][j])
            {
                deg[i]++;
                deg[j]++;
            }
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }
    for(int i=0;i<n;i++)
    {
        if(deg[i]%2==1)
            ch=0;
    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    vi ideg(n,0);
    vi odeg(n,0);
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
            if(v[i][j] && i!=j)
            {
                odeg[i]++;
                ideg[j]++;
            }
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }
    for(int i=0;i<n;i++)
    {
        if(ideg[i]<1 ||  odeg[i]<1)
            ch=0;
    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<n;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            trace1(i);
            ch=0;
            break;
        }

    }
    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            dfs(i,lev+1);

        }

    }
   vis[s]=0;
}
void rdfs(int s)
{
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[i][s])
        {
            vis[i]=1;
            if(cnt==n-1)
               { vis[i]=0;vis[s]=0; return ;}
            cnt++;
            rdfs(i);

        }

    }
   vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    vi ideg(n,0);
    vi odeg(n,0);
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];
            if(v[i][j] && i!=j)
            {
                odeg[i]++;
                ideg[j]++;
            }
        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        rdfs(i);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }

    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


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

#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define Max(x,y,z) max(x,max(y,z))
#define Min(x,y,z) min(x,min(y,z))
#define fr(i,s,e) for(i=s;i<e;i++)
#define rf(i,s,e) for(i=s-1;i>=e;i--)
#define pb push_back
#define eb emblace_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long
#define trace1(x)                cerr<<#x<<": "<<x<<endl
#define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
#define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
#define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
#define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
#define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
#define vl vector<long long>

#define vi vector<int>
#define vii vector< vector<int> >
#define vll vector< vector<long long> >
#define vpi vector< pair<ll,ll> >
#define MAXN 100001
#define mod %1000000007
vii v;
vi vis;
int n;
bool ch=1;
int cnt=0;
void dfs(int s,int lev)
{
   // trace3(s,lev,cnt);
    for(int i=0;i<n;i++)
    {
       // trace4(i,s,vis[i],v[s][i]);
        if(!vis[i] && v[s][i])
        {
            vis[i]=1;
            cnt++;
            dfs(i,lev+1);

        }

    }
   //vis[s]=0;
}
void rdfs(int s)
{
    for(int i=0;i<n;i++)
    {
        if(!vis[i] && v[i][s])
        {
            vis[i]=1;

            cnt++;
            rdfs(i);

        }

    }
  // vis[s]=0;
}
int main()
{
    IOS;
    #ifndef ONLINE_JUDGE
     freopen("int.txt","r",stdin);
     freopen("out.txt","w",stdout);
     #endif
  //  int n;
    cin>>n;
    v.resize(n,vi(n));
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>v[i][j];

        }
    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        dfs(i,0);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }
     vis.clear();
       vis.resize(n,0);
    for(int i=0;i<1;i++)
    {
       //trace1(i);
        vis[i]=1;
        cnt=0;
        rdfs(i);

        if(cnt!=n-1)
        {
            //trace1(i);
            ch=0;
            break;
        }

    }

    if(ch)
        cout<<"YES";
    else
        cout<<"NO";

    return 0;

}


#include <bits/stdc++.h>

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i,k) for(ll i=k;i>=0;i--)
#define rep(i,k) for(ll i=0;i<k;i++)
#define repn(i,k1,k2) for(ll i=k1;i<=k2;i++)

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert( B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001
#define read(type) readInt<type>()
#define tt ll t;cin>>t; while(t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr>vvpr;
typedef vector<vi> vvi;
typedef map<ll,ll> MPII;
typedef set<ll> SETI;
typedef multiset<ll> MSETI;

ll powmod(ll base,ll pow)
{
   ll res=1;
   while(pow)
   {
         if (pow%2!=0)  res=(res*base)%MOD;
         base=(base*base)%MOD;
         pow/=2;
   }
   return res;
}
 vvi g(ee5);vvi rev(ee5);
 int vis[ee5];
int o,cnt;
 void dfs(int v)
 {
 	vis[v]=1;
 	cnt++;
 	for(auto i:g[v])
 	{
 		if(vis[i])
 			continue;
 		dfs(i);
 	}
 }

int main()
{
	#if !ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n;cin>>n;
	rep(i,n)
	{
		rep(j,n)
		{
			{int x;cin>>x;if(x){if(i==j) o=i;g[i].pb(j);rev[j].pb(i);}}
		}
	}

	dfs(o);if(cnt!=n) {cout<<"NO";return 0;}cnt=0;mem(vis,0);g=rev;dfs(o);
	if(cnt==n)
		cout<<"YES";
	else
		cout<<"NO";

}


#include <bits/stdc++.h>

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i,k) for(ll i=k;i>=0;i--)
#define rep(i,k) for(ll i=0;i<k;i++)
#define repn(i,k1,k2) for(ll i=k1;i<=k2;i++)

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert( B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001
#define read(type) readInt<type>()
#define tt ll t;cin>>t; while(t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr>vvpr;
typedef vector<vi> vvi;
typedef map<ll,ll> MPII;
typedef set<ll> SETI;
typedef multiset<ll> MSETI;

ll powmod(ll base,ll pow)
{
   ll res=1;
   while(pow)
   {
         if (pow%2!=0)  res=(res*base)%MOD;
         base=(base*base)%MOD;
         pow/=2;
   }
   return res;
}
 vvi g(ee5);vvi rev(ee5);
 int vis[ee5];
int o,cnt;
 void dfs(int v)
 {
 	vis[v]=1;
 	{if(v!=o)cnt++;}
 	for(auto i:g[v])
 	{
 		if(vis[i])
 			continue;
 		dfs(i);
 	}
 }

int main()
{
	#if !ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n;cin>>n;
	rep(i,n)
	{
		rep(j,n)
		{
			{int x;cin>>x;if(x){if(i==j) o=i;g[i].pb(j);rev[j].pb(i);}}
		}
	}

	dfs(o);if(cnt!=n-1) {cout<<"NO";return 0;}g=rev;dfs(o);
	if(cnt==n-1)
		cout<<"YES";
	else
		cout<<"NO";

}


#include <bits/stdc++.h>

#if DEBUG && !ONLINE_JUDGE
    #include "debug.h"
#else
    #define debug(...)
#endif

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define scd(t) scanf("%d",&t)
#define scld(t) scanf("%ld",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define mem(a, b) memset(a, (b), sizeof(a))
#define repd(i,k) for(ll i=k;i>=0;i--)
#define rep(i,k) for(ll i=0;i<k;i++)
#define repn(i,k1,k2) for(ll i=k1;i<=k2;i++)

#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define foreach(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define in(A, B, C) assert( B <= A && A <= C)
#define mp make_pair
#define pb push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define ee6 (ll)1000001
#define ee5 (ll)100001
#define read(type) readInt<type>()
#define tt ll t;cin>>t; while(t--)
typedef long long int ll;

typedef pair<ll, ll> pr;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<pr> vpr;
typedef vector<vpr>vvpr;
typedef vector<vi> vvi;
typedef map<ll,ll> MPII;
typedef set<ll> SETI;
typedef multiset<ll> MSETI;

ll powmod(ll base,ll pow)
{
   ll res=1;
   while(pow)
   {
         if (pow%2!=0)  res=(res*base)%MOD;
         base=(base*base)%MOD;
         pow/=2;
   }
   return res;
}
 vvi g(ee5);vvi rev(ee5);
 int vis[ee5];
int o,cnt;
 void dfs(int v)
 {
 	vis[v]=1;
 	{if(v!=o)cnt++;}
 	for(auto i:g[v])
 	{
 		if(vis[i])
 			continue;
 		dfs(i);
 	}
 }

int main()
{
	#if !ONLINE_JUDGE
	freopen("in.txt","r",stdin);
	#endif
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	int n;cin>>n;
	rep(i,n)
	{
		rep(j,n)
		{
			{int x;cin>>x;if(x){if(i==j) o=i;g[i].pb(j);rev[j].pb(i);}}
		}
	}

	dfs(o);g=rev;dfs(o);
	if(cnt==n-1)
		cout<<"YES";
	else
		cout<<"NO";

}


#include <bits/stdc++.h>

#define long long long int
using namespace std;

#define Max 100005+5
// #define cons 1000000000+7
#define mp make_pair
#define pb push_back
#define INF 1e16
#define INF2 1e9+9
#define pi 3.141592653589
#define x first
#define y second

/*pair<int,int> pa[20][Max],level[Max];
//g++ -std=c++11 prac.cpp

int lca(int u,int v)
{
    if(level[u]>level[v])return lca(v,u);
    for(long i=19;i>=0 && level[v]!=level[u];i--)
    {
        if(level[v]>=level[u]+(1<<i))
            v=pa[i][v];
    }
    if(u==v)return u;
    for(long i=19;i>=0;i--)
    {
        if(pa[i][u]!=pa[i][v])
        {
            u=pa[i][u];v=pa[i][v];
        }
    }
    return pa[0][u];
}
*/

long cons;
long check(long a)
{
    if(a>=cons || a<=-cons)a%=cons;

    return a;
}
long check2(long a)
{
    if(a>0)return a;
    long b=a/cons;
    a-=b*cons;
    if(a<0)a+=cons;
    return a;
}

long GCD(long a,long b)
{
  if(b==0)
    return a;
  return GCD(b,a%b);
}

long exp(long a,long n)
{
    if(n==0)return 1;
    if (n==1)return check(a);

    long b=exp(a,n/2);

    if(n%2==0)return check(b*b);
    return check(b*check(b*a));
}

vector<int> graph[2002];
bool visited[2002];
int coun=0;
int dfsing;
bool flag;
int n;
void dfs(int i)
{
  coun++;
  visited[i]=true;

  for(auto itr:graph[i])
  {
    if(!flag)
      break;
    if(itr<dfsing)
    {
      flag=false;
      coun=n;
    }
    else
    {
      if(!visited[itr])
        dfs(itr);
    }
  }
}

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

   cons=1e9+7;

   // int n;
   cin>>n;

   bool flag2=true;

   for(int i=1;i<=n;i++)
   {
    for(int j=1;j<=n;j++)
    {
      int x;
      cin>>x;
      if(x)
        graph[i].pb(j);
      else
        flag2=false;
    }
   }

   if(flag2)
   {
    cout<<"YES";
    return 0;
   }

   for(int i=1;i<=n;i++)
   {
    coun=0;
    for(int j=1;j<=n;j++)
      visited[j]=false;
    dfsing=i;
    flag=true;
    dfs(i);

    if(coun<n)
    {
      cout<<"NO";
      return 0;
    }
   }

   cout<<"YES";
   return 0;
}


#include <bits/stdc++.h>

#define long long long int
using namespace std;

#define Max 100005+5
// #define cons 1000000000+7
#define mp make_pair
#define pb push_back
#define INF 1e16
#define INF2 1e9+9
#define pi 3.141592653589
#define x first
#define y second

/*pair<int,int> pa[20][Max],level[Max];
//g++ -std=c++11 prac.cpp

int lca(int u,int v)
{
    if(level[u]>level[v])return lca(v,u);
    for(long i=19;i>=0 && level[v]!=level[u];i--)
    {
        if(level[v]>=level[u]+(1<<i))
            v=pa[i][v];
    }
    if(u==v)return u;
    for(long i=19;i>=0;i--)
    {
        if(pa[i][u]!=pa[i][v])
        {
            u=pa[i][u];v=pa[i][v];
        }
    }
    return pa[0][u];
}
*/

long cons;
long check(long a)
{
    if(a>=cons || a<=-cons)a%=cons;

    return a;
}
long check2(long a)
{
    if(a>0)return a;
    long b=a/cons;
    a-=b*cons;
    if(a<0)a+=cons;
    return a;
}

long GCD(long a,long b)
{
  if(b==0)
    return a;
  return GCD(b,a%b);
}

long exp(long a,long n)
{
    if(n==0)return 1;
    if (n==1)return check(a);

    long b=exp(a,n/2);

    if(n%2==0)return check(b*b);
    return check(b*check(b*a));
}

vector<int> graph[2002];
bool visited[2002];
int coun=0;
int dfsing;
bool flag;
int n;
void dfs(int i)
{
  coun++;
  visited[i]=true;

  for(auto itr:graph[i])
  {
    if(!flag)
      break;
    if(itr<dfsing)
    {
      flag=false;
      coun=n;
    }
    else
    {
      if(!visited[itr])
        dfs(itr);
    }
  }
}

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

   cons=1e9+7;

   // int n;
   cin>>n;

   bool flag2=true;

   for(int i=1;i<=n;i++)
   {
    for(int j=1;j<=n;j++)
    {
      int x;
      cin>>x;
      if(x)
        graph[i].pb(j);
      else
        flag2=false;
    }
   }

   if(flag2)
   {
    cout<<"YES";
    return 0;
   }

   for(int i=1;i<=n;i++)
   {
    sort(graph[i].begin(),graph[i].end());
    coun=0;
    for(int j=1;j<=n;j++)
      visited[j]=false;
    dfsing=i;
    flag=true;
    dfs(i);

    if(coun<n)
    {
      cout<<"NO";
      return 0;
    }
   }

   cout<<"YES";
   return 0;
}


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

int adj[2000][2000] = {0}, n;
bool vis[2000] = {0};
int last = 0;

int dfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[a][i] and not vis[i]) ret += dfs(i);
  return ret;
}
int rdfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[i][a] and not vis[i]) ret += dfs(i);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    cin >> adj[i][j];
  }
  cout <<
  (dfs(0) == n and (memset(vis, 0, sizeof(vis)), rdfs(last) == n) ? "YES\n" : "NO\n");

}


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

struct Dsu {
  struct node_struct {
    int par, size;
    node_struct(int p, int s) : par(p), size(s) {}
    void merge(node_struct &b) {
      b.par = par;
      size += b.size;
    }
  };
  int compo;
  vector<node_struct> nodes;
  stack<tuple<int, int, node_struct, node_struct>> stk;
  Dsu(int n) {
    nodes.reserve(n);
    compo = n;
    for (int i = 0; i < n; ++i) nodes.emplace_back(i, 1);
  }
  int anc(int x) {
    while (x != nodes[x].par) x = nodes[x].par;
    return x;
  }
  bool unite(int x, int y) {
    int a = anc(x);
    int b = anc(y);
    stk.emplace(a, b, nodes[a], nodes[b]);
    if (a == b) return false;
    if (nodes[a].size < nodes[b].size) swap(a, b);
    nodes[a].merge(nodes[b]);
    --compo;
    return true;
  }
  void revert(int version = -1) {  // 0 index
    if (version == -1) version = stk.size() - 1;
    for (; stk.size() != version; stk.pop()) {
      nodes[get<0>(stk.top())] = get<2>(stk.top());
      nodes[get<1>(stk.top())] = get<3>(stk.top());
    }
  }
};

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  int n; cin >> n;
  Dsu djs(n);
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    int a; cin >> a;
    if (a) djs.unite(i, j);
  }
  cout << (djs.compo == 1 ? "YES\n" : "NO\n");

}


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

int adj[2000][2000] = {0}, n;
bool vis[2000] = {0};
int last = 0;

int dfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[a][i] and not vis[i]) ret += dfs(i);
  last = a;
  return ret;
}
int rdfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[i][a] and not vis[i]) ret += rdfs(i);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    cin >> adj[i][j];
  }
  cout <<
  (dfs(0) == n and (memset(vis, 0, sizeof(vis)), rdfs(last) == n) ? "YES\n" : "NO\n");

}


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

int adj[2000][2000] = {0}, n;
bool vis[2000] = {0};
int last = 0;

int dfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[a][i] and not vis[i]) ret += dfs(i);
  last = a;
  return ret;
}
int rdfs(int a) {
  vis[a] = 1;
  int ret = 1;
  for (int i = 0; i < n; ++i) if (adj[i][a] and not vis[i]) ret += dfs(i);
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false); cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) {
    cin >> adj[i][j];
  }
  cout <<
  (dfs(0) == n and (memset(vis, 0, sizeof(vis)), rdfs(last) == n) ? "YES\n" : "NO\n");

}


#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 2000 + 10;
bool state[MAXN][MAXN];
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    int val;
    queue<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val != 0) state[i][j] = true;
            else q.push({i, j});
        }
    }
    while (!q.empty()) {
        bool done = true;
        queue<pii> nxt;
        while (!q.empty()) {
            pii item = q.front(); q.pop();
            int i = item.first, j = item.second;
            if (state[i][j]) continue;
            for (int k = 0; k < n; k++) {
                if (state[i][k] && state[k][j]) {
                    done = false;
                    state[i][j] = true;
                    break;
                }
            }
            if (!state[i][j]) nxt.push({i, j});
        }
        if (done) break;
        swap(nxt, q);
    }
    bool ret = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (!state[i][j]) {
                ret = false;
                break;
            }
        }
        if (!ret) break;
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}


#include <iostream>
using namespace std;
const int MAXN = 2000 + 10;
bool state[MAXN][MAXN];
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    int val;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val != 0) state[i][j] = true;
        }
    }
    while (true) {
        bool done = true;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!state[i][j]) {
                    for (int k = 0; k < n; k++) {
                        if (state[i][k] && state[k][j]) {
                            done = false;
                            state[i][j] = true;
                            break;
                        }
                    }
                }
            }
        }
        if (done) break;
    }
    bool ret = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (!state[i][j]) {
                ret = false;
                break;
            }
        }
        if (!ret) break;
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}


#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
const int MAXN = 2000 + 10;
bool vis[MAXN], state[MAXN][MAXN];
int n;
void dfs (int cur, bool reverse = false) {
    vis[cur] = true;
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        if (!reverse && state[cur][i]) {
            dfs(i, reverse);
        }
        else if (reverse && state[i][cur]) {
            dfs(i, reverse);
        }
    }
}
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    int val, s = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val) state[i][j] = true;
        }
        if (state[i][i]) s = i;
    }
    bool ret = true;
    dfs (s);
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            ret = false;
            break;
        }
    }
    if (ret) {
        memset(vis, false, sizeof(vis));
        dfs(s, true);
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                ret = false;
                break;
            }
        }
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
}


#include <iostream>
#include <queue>
#include <memory.h>
using namespace std;
const int MAXN = 2000 + 10;
bool vis[MAXN], state[MAXN][MAXN];
int n;
void dfs (int cur, bool reverse = false) {
    vis[cur] = true;
    for (int i = 0; i < n; i++) {
        if (vis[i]) continue;
        if (!reverse && state[cur][i]) {
            dfs(i);
        }
        else if (reverse && state[i][cur]) {
            dfs(i);
        }
    }
}
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin >> n;
    int val, s = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val) state[i][j] = true;
        }
        if (state[i][i]) s = i;
    }
    bool ret = true;
    dfs (s);
    for (int i = 0; i < n; i++) {
        if (!vis[i]) {
            ret = false;
            break;
        }
    }
    if (ret) {
        memset(vis, false, sizeof(vis));
        dfs(s, true);
        for (int i = 0; i < n; i++) {
            if (!vis[i]) {
                ret = false;
                break;
            }
        }
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
}


#include <iostream>
using namespace std;
const int MAXN = 2000 + 10;
bool state[MAXN][MAXN];
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    int val;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val != 0) state[i][j] = true;
        }
    }
    while (true) {
        bool done = true;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (!state[i][j]) {
                    for (int k = 0; k < n; k++) {
                        if (state[i][k] && state[k][j]) {
                            done = false;
                            state[i][j] = true;
                        }
                    }
                }
            }
        }
        if (done) break;
    }
    bool ret = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (!state[i][j]) {
                ret = false;
                break;
            }
        }
        if (!ret) break;
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}


#include <iostream>
#include <queue>
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 2000 + 10;
bool state[MAXN][MAXN];
int main() {
//    freopen("test.txt", "r", stdin);
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n; cin >> n;
    int val;
    queue<pii> q;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> val;
            if (val != 0) {
                state[i][j] = true;
                q.push({i, j});
            }
        }
    }
    while (!q.empty()) {
        pii item = q.front(); q.pop();
        int i = item.first, j = item.second;
        for (int k = 0; k < n; k++) {
            if (state[j][k] && !state[i][k]) {
                state[i][k] = true;
                q.push({i, k});
            }
            if (state[k][i] && !state[k][j]) {
                state[k][j] = true;
                q.push({k, j});
            }
        }
    }
    bool ret = true;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (!state[i][j]) {
                ret = false;
                break;
            }
        }
        if (!ret) break;
    }
    if (ret) cout << "YES" << endl;
    else cout << "NO" << endl;
    return 0;
}


//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=2000+10,maxn=1000000+10,inf=0x3f3f3f3f;

vi v[N];
int dfn[N],low[N],cnt,in[N],num;
stack<int>s;
void tarjan(int u)
{
    in[u]=1;s.push(u);
    dfn[u]=low[u]=++cnt;
    for(int x:v[u])
    {
//        if(u==2)printf("%d \n",x);
        if(!dfn[x])
        {
            tarjan(x);
            low[u]=min(low[u],low[x]);
        }
        else if(in[x]==1)low[u]=min(low[u],dfn[x]);
    }
    if(dfn[u]==low[u])
    {
        num++;
        while(!s.empty())
        {
            int x=s.top();s.pop();
            in[x]=2;
            if(x==u)break;
        }
    }
//    printf("%d %d %d\n",u,dfn[u],low[u]);
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
    {
        int x;scanf("%d",&x);
        if(x&&i!=j)v[i].pb(j);
    }
    for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i);
    puts(num==1?"YES":"NO");
    return 0;
}
/********************

********************/


//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
//#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
#define bpc __builtin_popcount
#define base 1000000000000000000ll
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
#define mr mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;}

using namespace std;
//using namespace __gnu_pbds;

const ull ba=233;
const db eps=1e-5;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=2000+10,maxn=1000000+10,inf=0x3f3f3f3f;

vi v[N];
int dfn[N],low[N],cnt,in[N],num;
stack<int>s;
void tarjan(int u,int f)
{
    in[u]=1;s.push(u);
    dfn[u]=low[u]=++cnt;
    for(int x:v[u])if(x!=f)
    {
        if(!dfn[x])
        {
            tarjan(x,u);
            low[u]=min(low[u],low[x]);
        }
        else if(in[x]==1)low[u]=min(low[u],dfn[x]);
    }
    if(dfn[u]==low[u])
    {
        num++;
        while(!s.empty())
        {
            int x=s.top();s.pop();
            in[x]=2;
            if(x==u)break;
        }
    }
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
    {
        int x;scanf("%d",&x);
        if(x)v[i].pb(j);
    }
    for(int i=1;i<=n;i++)if(!dfn[i])tarjan(i,-1);
    puts(num==1?"YES":"NO");
    return 0;
}
/********************

********************/


#include <bits/stdc++.h>
using namespace std;
const int maxv = 2e3+10;
int V;
bool G[maxv][maxv], rG[maxv][maxv], used[maxv];
int vs[maxv], tot;
int cmp[maxv];

void add_edge(int from, int to)
{
	G[from][to] = 1;
	rG[to][from] = 1;
}

void dfs(int v)
{
	used[v] = true;
	for(int i=0; i<V; ++i)
	{
		if(G[v][i] && !used[i]) dfs(i);
	}
	vs[tot++] = v;
}

void rdfs(int v, int k)
{
	used[v] = true;
	cmp[v] = k;
	for(int i=0; i<V; ++i)
	{
		if(rG[v][i] && !used[i]) rdfs(i, k);
	}
}

int scc()
{
	memset(used, 0, sizeof(used));
	tot = 0;
	for(int v=0; v<V; ++v) if(!used[v]) dfs(v);
	memset(used, 0, sizeof(used));
	int k=0;
	for(int i=tot-1; i>=0; --i)
		if(!used[vs[i]]) rdfs(vs[i], k++);
	return k;
}

int main()
{
	scanf("%d", &V);
	int t;
	for(int i=0; i<V; ++i)
	{
		for(int j=0; j<V; ++j)
		{
			scanf("%d", &t);
			if(t) add_edge(i, j);
		}
	}
	if(scc() == 1) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}

// 12 16
// 12 11
// 11 10
// 11 8
// 8 10
// 10 9
// 9 8
// 9 7
// 5 7
// 7 6
// 6 5
// 6 3
// 6 4
// 4 3
// 3 2
// 2 3
// 4 1


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

const int maxv = 3000;
int V;
vector<int> G[maxv], rG[maxv];
vector<int> vs;
bool used[maxv];
int cmp[maxv];

void add_edge(int from, int to)
{
	G[from].push_back(to);
	rG[to].push_back(from);
}
void dfs(int v)
{
	used[v] = true;
	for(auto ite : G[v])
	{
		if(!used[ite]) dfs(ite);
	}
	vs.push_back(v);
}
void rdfs(int v, int k)
{
	used[v] = true;
	cmp[v] = k;
	for(auto ite : rG[v])
	{
		if(!used[ite]) rdfs(ite, k);
	}
}

int scc()
{
	memset(used, 0, sizeof(used));
	vs.clear();
	for(int v=0; v<V; ++v) if(!used[v]) dfs(v);
	memset(used, 0, sizeof(used));
	int k = 0;
	for(int i=vs.size()-1; i>=0; --i) if(!used[vs[i]]) rdfs(vs[i], k++);
	return k;
}

int main()
{
	cin >> V;
	int t;
	for(int i=0; i<V; ++i)
	{
		for(int j=0; j<V; ++j)
		{
			scanf("%d", &t);
			if(t) add_edge(i, j);
		}
	}
	if(scc() == 1) cout << "YES" << endl;
	else cout << "NO" << endl;

	return 0;
}

// 12 16
// 12 11
// 11 10
// 11 8
// 8 10
// 10 9
// 9 8
// 9 7
// 5 7
// 7 6
// 6 5
// 6 3
// 6 4
// 4 3
// 3 2
// 2 3
// 4 1


#include <bits/stdc++.h>
using namespace std;
const int maxv = 2e3+10;
int V;
bool G[maxv][maxv], rG[maxv][maxv], used[maxv];
int vs[maxv], tot;
int cmp[maxv];

void add_edge(int from, int to)
{
	G[from][to] = 1;
	rG[to][from] = 1;
}

void dfs(int v)
{
	used[v] = true;
	for(int i=0; i<V; ++i)
	{
		if(G[v][i] && !used[i]) dfs(i);
	}
	vs[tot++] = v;
}

void rdfs(int v, int k)
{
	used[v] = true;
	cmp[v] = k;
	for(int i=0; i<V; ++i)
	{
		if(rG[v][i] && !used[i]) rdfs(i, k);
	}
}

int scc()
{
	for(int v=0; v<V; ++v) if(!used[v]) dfs(v);
	memset(used, 0, sizeof(bool)*V);
	int k=0;
	for(int i=tot-1; i>=0; --i)
		if(!used[vs[i]]) rdfs(vs[i], k++);
	return k;
}

int main()
{
	scanf("%d", &V);
	int t;
	for(int i=0; i<V; ++i)
	{
		for(int j=0; j<V; ++j)
		{
			scanf("%d", &t);
			if(t) add_edge(i, j);
		}
	}
	if(scc() == 1) puts("YES");
	else puts("NO");

	return 0;
}


#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std;

#define SET(p) memset(p, -1, sizeof(p))
#define CLR(p) memset(p, 0, sizeof(p))
#define MEM(p, v) memset(p, v, sizeof(p))
#define CPY(d, s) memcpy(d, s, sizeof(s))
#define ll long long
#define ld long double
#define mod 1000000007
#define inf 1LL<<60
#define pii pair< int, int >
#define pll pair< ll, ll >
#define psi pair< string, int >
#define N 2001
int a[N][N];
bool vis[N];
int n;
int cnt1,cnt2;
void go(int cur){
	vis[cur] = true;
	cnt1++;
	for(int i=0;i<n;i++){
		if(!vis[i] && a[cur][i]){
			go(i);
		}
	}
}
void go1(int cur){
	vis[cur] = true;
	cnt2++;
	for(int i=0;i<n;i++){
		if(!vis[i] && a[i][cur]){
			go1(i);
		}
	}
}
int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(false);
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
		}
	}
	go(1);CLR(vis);go1(1);
	if(cnt1!=n || cnt2!=n){
		printf("NO\n");
	}
	else{
		printf("YES\n");
	}
	return 0;
}