#include<bits/stdc++.h>
using namespace std;
const int Nmax = (1 << 20) + 7;
const int nodemax = 507;
const int MOD = 1000000007;
int numnode, k, len;
long long edge[nodemax][nodemax];
vector<int> ship;
long long dp[23][Nmax];
void in(){
memset(dp, -1, sizeof(dp));
cin >> numnode >> k;
for(int i = 1; i <= numnode; i++){
for(int j = 1; j <= numnode; j++){
int x;
cin >> x;
edge[i][j] = x;
}
}
for(int i = 1; i <= k; i++){
int x; cin >> x;
ship.push_back(x);
} sort(ship.begin(), ship.end());
len = ship.size();
for(int k = 1; k <= numnode; k++){
for(int i = 1 ; i <= numnode; i++){
for(int j = 1 ; j <= numnode; j++){
if(edge[i][j] > edge[i][k] + edge[k][j]) edge[i][j] = edge[i][k] + edge[k][j];
}
}
}
}
long long calc(int u, int choose){
if(choose == (1 << len) - 1){
long long res = edge[u][1];
return res;
}
long long &res = dp[u][choose];
if(res != -1) return res;
res = 1e18;
for(int i = 0; i < len; i++){
int v = ship[i];
int c = edge[u][v];
if(!((choose >> i) & 1)) {
res = min(res, 0ll + c + calc(v, choose + (1 << i)));
}
}
return res;
}
void sol(){
cout << calc(1, 0);
}
int main(){
freopen("banhkhuc.inp", "r", stdin);
freopen("banhkhuc.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
in();
sol();
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmNvbnN0IGludCBObWF4ID0gKDEgPDwgMjApICsgNzsKY29uc3QgaW50IG5vZGVtYXggPSA1MDc7CmNvbnN0IGludCBNT0QgPSAxMDAwMDAwMDA3OwoKaW50IG51bW5vZGUsIGssIGxlbjsKbG9uZyBsb25nIGVkZ2Vbbm9kZW1heF1bbm9kZW1heF07CnZlY3RvcjxpbnQ+IHNoaXA7CmxvbmcgbG9uZyBkcFsyM11bTm1heF07CnZvaWQgaW4oKXsKICAgIG1lbXNldChkcCwgLTEsIHNpem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