#include<bits/stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f;
int n,k,m,s,t;
int g[105][105],wh[105];
bool pc[105][105],x[105];
int main(){
scanf("%d%d%d%d%d",&n,&k,&m,&s,&t);
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
g[i][j]=INF;
g[i][i]=0;
}
}
for(int i=1;i<=n;++i){
int c;
scanf("%d",&c);
wh[i]=c;
}
for(int i=1;i<=k;++i){
for(int j=1;j<=k;++j){
int a;
scanf("%d",&a);
if(a||i==j){
pc[i][j]=true;
}
}
}
for(int i=1;i<=m;++i){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(!pc[wh[u]][wh[v]])g[v][u]=w;
if(!pc[wh[v]][wh[u]])g[u][v]=w;
}
x[wh[s]]=true;
for(int k=1;k<=n;++k){
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(g[i][j]>g[i][k]+g[k][j]&&(!x[wh[j]])&&(!x[wh[k]])){
g[i][j]=g[i][k]+g[k][j];
x[wh[j]]=x[wh[k]]=true;
}
}
}
}
if(g[s][t]==INF){
printf("-1");
}
else{
printf("%d",g[s][t]);
}
return 0;
}