import java.util.*;
import java.lang.*;
import java.io.*;
class Ideone
{
public static void main
(String[] args
) {
Scanner scan
= new Scanner
(System.
in); int tam = scan.nextInt();
int inicio = scan.nextInt();
int fim= scan.nextInt();
int[][] ma = new int[8][8];
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
ma[i][j] = scan.nextInt();
}
}
scan.close();
int distancias[] = buscaLateral(inicio, ma, 8);
System.
out.
println("Início: " + inicio
); System.
out.
println("Fim: " + fim
); System.
out.
println("Menor distância do início ao fim: " + distancias
[fim
]); }
public static int[] buscaLateral(int inicio, int[][] ma, int tam){
int[] nivel = new int[tam];
Queue<Integer> fila = new LinkedList<Integer>();
// Inicialinzando nivel de todos os vértices como -1
for(int i=0; i<8; i++)
nivel[i]=-1;
// O nível do vértice inicial é zero
nivel[inicio] = 0;
fila.add(inicio);
// Executando a busca lateral
while(!fila.isEmpty()){
int atual = fila.remove();
for(int i = 0; i < 8; i++)
if(ma(atual, i, ma) == 1 && nivel[i] == -1){
nivel[i] = nivel[atual] + 1;
fila.add(i);
}
}
return nivel;
}
public static int ma(int i, int j, int[][] ma){
if(i < j)
return ma[i][j];
else
return ma[j][i];
}
}
aW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5sYW5nLio7CmltcG9ydCBqYXZhLmlvLio7CgpjbGFzcyBJZGVvbmUKewoJCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKFN0cmluZ1tdIGFyZ3MpIHsKCQkJCgkJCVNjYW5uZXIgc2NhbiA9IG5ldyBTY2FubmVyKFN5c3RlbS5pbik7CgkJCWludCB0YW0gPSBzY2FuLm5leHRJbnQoKTsKCQkJaW50IGluaWNpbyA9IHNjYW4ubmV4dEludCgpOwoJCQlpbnQgZmltPSBzY2FuLm5leHRJbnQoKTsKCQkJaW50W11bXSBtYSA9IG5ldyBpbnRbOF1bOF07CgkJICAgIGZvciAoaW50IGkgPSAwOyBpIDwgODsgKytpKSB7CgkJICAgICAgICBmb3IgKGlud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