#include <iostream>
#include <cmath>
#include <iomanip>
#include <vector>
using namespace std;
void dfs(int cur, vector<vector<int>> & adj, vector<bool> & was, int & count){
if(!was[cur]){
was[cur] = true;
count++;
for(int i = 0; i < adj[cur].size(); i++){
dfs(adj[cur][i], adj, was, count);
}
}
}
int getCount(int start, vector<vector<int>> & adj){
vector<bool> was(adj.size(), false);
int count = 0;
dfs(start, adj, was, count);
return count;
}
int main() {
int n;
cin >> n;
int start;
cin >> start;
start--;
vector<vector<int>> adj(n, vector<int>());
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
int temp;
cin >> temp;
if(temp == 1){
adj[i].push_back(j);
}
}
}
cout << getCount(start, adj);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y21hdGg+CiNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8dmVjdG9yPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCnZvaWQgZGZzKGludCBjdXIsIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gJiBhZGosIHZlY3Rvcjxib29sPiAmIHdhcywgaW50ICYgY291bnQpewogICAgaWYoIXdhc1tjdXJdKXsKICAgICAgICB3YXNbY3VyXSA9IHRydWU7CiAgICAgICAgY291bnQrKzsKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgYWRqW2N1cl0uc2l6ZSgpOyBpKyspewogICAgICAgICAgICBkZnMoYWRqW2N1cl1baV0sIGFkaiwgd2FzLCBjb3VudCk7CiAgICAgICAgfQogICAgfQp9CgppbnQgZ2V0Q291bnQoaW50IHN0YXJ0LCB2ZWN0b3I8dmVjdG9yPGludD4+ICYgYWRqKXsKICAgIHZlY3Rvcjxib29sPiB3YXMoYWRqLnNpemUoKSwgZmFsc2UpOwogICAgaW50IGNvdW50ID0gMDsKICAgIGRmcyhzdGFydCwgYWRqLCB3YXMsIGNvdW50KTsKICAgIHJldHVybiBjb3VudDsKfQoKaW50IG1haW4oKSB7CiAgICBpbnQgbjsKICAgIGNpbiA+PiBuOwogICAgaW50IHN0YXJ0OwogICAgY2luID4+IHN0YXJ0OwogICAgc3RhcnQtLTsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gYWRqKG4sIHZlY3RvcjxpbnQ+KCkpOwogICAgZm9yKGludCBpID0gMDsgaSA8IG47IGkrKyl7CiAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IG47IGorKyl7CiAgICAgICAgICAgIGludCB0ZW1wOwogICAgICAgICAgICBjaW4gPj4gdGVtcDsKICAgICAgICAgICAgaWYodGVtcCA9PSAxKXsKICAgICAgICAgICAgICAgIGFkaltpXS5wdXNoX2JhY2soaik7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGdldENvdW50KHN0YXJ0LCBhZGopOwogICAgcmV0dXJuIDA7Cn0=