#include<bits/stdc++.h>
#define MAXN 1000005
using namespace std;
stack <int> s;
struct node {
int nxt,to;
}t[MAXN];
int head[MAXN],dfn[MAXN],low[MAXN],vis[MAXN],used[MAXN],a[1005][1005],co_id[MAXN],cnt = 0,tot = 0,co = 0;
void add(int x,int y) {
t[++cnt].nxt = head[x];
t[cnt].to = y;
head[x] = cnt;
}
void tarjan(int u) {
dfn[u] = low[u] = ++tot;
used[u] = vis[u] = 1;
s.push(u);
for(int i = head[u];i;i = t[i].nxt) {
int v = t[i].to;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u],low[v]);
}
else if(vis[v]) {
low[u] = min(low[u],dfn[v]);
}
}
if(low[u] == dfn[u]) {
co ++;
while(s.top() != u) {
vis[s.top()] = 0;
s.pop();
}
vis[s.top()] = 0;
s.pop();
}
}
int main()
{
int n;
scanf("%d",&n);
for(int i = 1;i <= n;i ++) {
for(int j = 1;j <= n;j ++) {
scanf("%d",&a[i][j]);
add(i,j);
}
}
for(int i = 1;i <= n;i ++) {
if(!used[i]) {
tarjan(i);
}
}
printf("%d",co);
return 0;
}