#include
using namespace std;
int n, g[101][101], d[101], s, f;
bool used[101];
queue q;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
cin >> g[i][j];
}
}
cin >> s >> f;
used[s] = true;
d[s] = 0;
q.push(s);
while (!q.empty()) {
int v = q.front();
q.pop();
for (int to = 1; to <= n; ++to) {
if (g[v][to] == 1 && !used[to]) {
used[to] = true;
d[to] = d[v] + 1;
q.push(to);
}
}
}
if (!used[f]) {
cout << -1;
} else {
cout << d[f];
}
return 0;
}