#include "bits/stdc++.h"
#define puba push_back
#define mapa make_pair
#define ff first
#define ss second
#define bend(_x) (_x).begin(), (_x).end()
#define szof(_x) ((int) (_x).size())

using namespace std;
typedef long long ll;
typedef double dbl;
typedef unsigned int ui;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <long long, long long> pll;
const int MAXN = 21;
const int INF = 1e9;

int n, k;
int graph[MAXN][MAXN];
int to0[MAXN];
int num[MAXN];

//bitset <1 << MAXN> used[MAXN];
int d[MAXN][1 << MAXN];

int main() {    
    freopen("run.in", "r", stdin);
    freopen("run.out", "w", stdout);                       

    //cin >> n >> k;
    scanf("%d%d", &n, &k);

    for (int i = 0; i < k; ++i) {
        //cin >> num[i];
        scanf("%d", &num[i]);
    }

    int bal = 0;
    for (int i = 0; i < k; ++i) {
        bal += num[i];
        to0[bal] = 1;
    }

    for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
            //cin >> graph[i][j];
            scanf("%d", &graph[i][j]);
        }
    }

    for (int i = 0; i < MAXN; ++i) {
        for (int j = 0; j < (1 << MAXN); ++j) {
            d[i][j] = INF;
        }
    }

    d[0][0] = 0;

    int bdv = 1 << n;

    for (int bits = 0; bits < bdv; ++bits) {
        for (int j = 0; j <= n; ++j) {
            int tmp = __builtin_popcount(bits);
            //cerr << bits << " " << j << " " << d[j][bits] << endl;

            if (!to0[tmp + 1]) {
                for (int i = 0; i < n; ++i) {
                    if (!(bits & (1 << i))) {
                        //pq.push(mapa(mapa(p.ff.ff - graph[p.ss][i + 1], bits | (1 << i)), i + 1));
                        d[i + 1][bits | (1 << i)] = min(d[i + 1][bits | (1 << i)], d[j][bits] + graph[j][i + 1]);
                    }
                }
            } else {
                for (int i = 0; i < n; ++i) {
                    if (!(bits & (1 << i))) {
                        //pq.push(mapa(mapa(p.ff.ff - graph[p.ss][i + 1] - graph[i + 1][0], bits | (1 << i)), 0));
                        d[0][bits | (1 << i)] = min(d[0][bits | (1 << i)], d[j][bits] + graph[j][i + 1] + graph[i + 1][0]);
                    }
                }
            }
        }
    }

    cout << d[0][(1 << n) - 1] << endl;

    return 0;
}