#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;
}