#include <iostream>
#include <queue>
using namespace std;
int main() {
int n, x, y, v;
cin >> n;
int matrix[n + 1][n + 1], dist[n + 1];
queue <int> plan;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
cin >> matrix[i][j];
cin >> x >> y;
for (int i = 1; i <= n; i++)
dist[i] = -1;
plan.push(x);
dist[x] = 0;
while (!plan.empty()) {
v = plan.front();
plan.pop();
for (int i = 1; i <= n; i++) {
if (dist[i] == -1 && matrix[v][i]) {
plan.push(i);
dist[i] = dist[v] + 1;
}
}
if (v == y) plan.empty(); // если обработали вершину y, прекращаем обход
}
cout << dist[y];
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cXVldWU+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCWludCBuLCB4LCB5LCB2OwoJY2luID4+IG47CglpbnQgbWF0cml4W24gKyAxXVtuICsgMV0sIGRpc3RbbiArIDFdOyAKCXF1ZXVlIDxpbnQ+IHBsYW47Cglmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIAoJCWZvciAoaW50IGogPSAxOyBqIDw9IG47IGorKykKCQkJY2luID4+IG1hdHJpeFtpXVtqXTsKCWNpbiA+PiB4ID4+IHk7Cglmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCgkJZGlzdFtpXSA9IC0xOwoJcGxhbi5wdXNoKHgpOwoJZGlzdFt4XSA9IDA7Cgl3aGlsZSAoIXBsYW4uZW1wdHkoKSkgewoJCXYgPSBwbGFuLmZyb250KCk7CgkJcGxhbi5wb3AoKTsKCQlmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKCQkJaWYgKGRpc3RbaV0gPT0gLTEgJiYgbWF0cml4W3ZdW2ldKSB7CgkJCQlwbGFuLnB1c2goaSk7CgkJCQlkaXN0W2ldID0gZGlzdFt2XSArIDE7CgkJCX0KCQl9CgkJaWYgKHYgPT0geSkgcGxhbi5lbXB0eSgpOyAvLyDQtdGB0LvQuCDQvtCx0YDQsNCx0L7RgtCw0LvQuCDQstC10YDRiNC40L3RgyB5LCDQv9GA0LXQutGA0LDRidCw0LXQvCDQvtCx0YXQvtC0Cgl9IAoJY291dCA8PCBkaXN0W3ldOwoJcmV0dXJuIDA7Cn0=