#include <iostream>
#include <queue>
using namespace std;
int graph[1001][1001], d[1001];
queue <int> q;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, x, v;
cin >> n >> x;
x--;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> graph[i][j];
}
}
for (int i = 0; i < n; i++) {
d[i] = -1;
}
q.push(x);
d[x] = 0;
while (!q.empty()) {
v = q.front();
q.pop();
for (int i = 0; i < n; i++) {
if (d[i] == -1 && graph[v][i]) {
q.push(i);
d[i] = d[v] + 1;
}
}
}
for(int i = 0; i < n; i ++) {
cout << d[i] << " ";
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBncmFwaFsxMDAxXVsxMDAxXSwgZFsxMDAxXTsKcXVldWUgPGludD4gcTsKIAppbnQgbWFpbigpIHsKCWlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOwoJY2luLnRpZShOVUxMKTsKICAgIGludCBuLCB4LCB2OwogICAgY2luID4+IG4gPj4geDsKICAgIHgtLTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBuOyBqKyspIHsKICAgICAgICAgICAgY2luID4+IGdyYXBoW2ldW2pdOwogICAgICAgIH0KICAgIH0KICAgIGZvciAoaW50IGkgPSAwOyBpIDwgbjsgaSsrKSB7CiAgICAgICAgZFtpXSA9IC0xOwogICAgfQogICAgcS5wdXNoKHgpOwogICAgZFt4XSA9IDA7CiAgICB3aGlsZSAoIXEuZW1wdHkoKSkgewogICAgICAgIHYgPSBxLmZyb250KCk7CiAgICAgICAgcS5wb3AoKTsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewogICAgICAgICAgICBpZiAoZFtpXSA9PSAtMSAmJiBncmFwaFt2XVtpXSkgewogICAgICAgICAgICAgICAgcS5wdXNoKGkpOwogICAgICAgICAgICAgICAgZFtpXSA9IGRbdl0gKyAxOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfSAKICAgIGZvcihpbnQgaSA9IDA7IGkgPCBuOyBpICsrKSB7CiAgICAJY291dCA8PCBkW2ldIDw8ICIgIjsKICAgIH0KICAgIHJldHVybiAwOwp9