#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <utility>
#include <numeric>
#include <cstring>
#include <cmath>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iomanip>
#include <sstream>
#include <cassert>
using namespace std;
#define y0 fsdjkfsdkf
#define y1 gfsdkjfsdjk
#define next fsdkjfsdjk
#define link dfkfsdjkfsk
int n, k, i, j, h, nx;
int a[200], g[200];
bool mar[200];
int d[200][200];
int bb[1<<19];
int f[320000][19];
int calc(int x) {
int ret = 0;
while (x) {
ret += (x & 1);
x /= 2;
}
return ret;
}
int main() {
freopen("run.in", "r", stdin);
freopen("run.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
for (int i = 1; i <= k; i++) {
cin >> a[i];
g[i] = g[i - 1] + a[i];
mar[g[i]] = 1;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= n; j++) {
cin >> d[i][j];
}
}
h = (1 << n) - 1;
for (int i = 0; i <= h; i++)
for (int j = 0; j <= n; j++) f[i][j] = (int)1e9;
for (int i = 0; i <= h; i++) bb[i] = calc(i);
f[0][0] = 0;
for (int i = 0; i <= h; i++)
for (int j = 0; j <= n; j++) if (f[i][j] < (int)1e9) {
for (int nx = 1; nx <= n; nx++) {
if (i & (1 << (nx - 1))) continue;
if (mar[bb[i] + 1]) {
f[i ^ (1 << (nx - 1))][0] = min(f[i ^ (1 << (nx - 1))][0], f[i][j] + d[j][nx] + d[nx][0]);
} else {
f[i ^ (1 << (nx - 1))][nx] = min(f[i ^ (1 << (nx - 1))][nx], f[i][j] + d[j][nx]);
}
}
}
cout << f[h][0] << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgojaW5jbHVkZSA8Y3N0ZGxpYj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHV0aWxpdHk+CiNpbmNsdWRlIDxudW1lcmljPgojaW5jbHVkZSA8Y3N0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HsKICAgICAgICBmb3IgKGludCBqID0gMDsgaiA8PSBuOyBqKyspIHsKICAgICAgICAgICAgY2luID4+IGRbaV1bal07CiAgICAgICAgfQogICAgfQogICAgaCA9ICgxIDw8IG4pIC0gMTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IGg7IGkrKykKICAgIGZvciAoaW50IGogPSAwOyBqIDw9IG47IGorKykgZltpXVtqXSA9IChpbnQpMWU5OwogICAgZm9yIChpbnQgaSA9IDA7IGkgPD0gaDsgaSsrKSBiYltpXSA9IGNhbGMoaSk7CiAgICBmWzBdWzBdID0gMDsKICAgIGZvciAoaW50IGkgPSAwOyBpIDw9IGg7IGkrKykKICAgIGZvciAoaW50IGogPSAwOyBqIDw9IG47IGorKykgaWYgKGZbaV1bal0gPCAoaW50KTFlOSkgewogICAgICAgIGZvciAoaW50IG54ID0gMTsgbnggPD0gbjsgbngrKykgewogICAgICAgICAgICBpZiAoaSAmICgxIDw8IChueCAtIDEpKSkgY29udGludWU7CiAgICAgICAgICAgIGlmIChtYXJbYmJbaV0gKyAxXSkgewogICAgICAgICAgICAgICBmW2kgXiAoMSA8PCAobnggLSAxKSldWzBdID0gbWluKGZbaSBeICgxIDw8IChueCAtIDEpKV1bMF0sIGZbaV1bal0gKyBkW2pdW254XSArIGRbbnhdWzBdKTsKICAgICAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgICAgZltpIF4gKDEgPDwgKG54IC0gMSkpXVtueF0gPSBtaW4oZltpIF4gKDEgPDwgKG54IC0gMSkpXVtueF0sIGZbaV1bal0gKyBkW2pdW254XSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0IDw8IGZbaF1bMF0gPDwgZW5kbDsKCiAgICByZXR1cm4gMDsKfQ==