#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;

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

//bitset <1 << MAXN> used[MAXN];
bool used[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]);
        }
    }

    priority_queue <pair <pii, int>> pq;

    pq.push(mapa(mapa(0, 0), 0));

    while (szof(pq)) {
        pair <pii, int> p;
        do {
            p = pq.top();
            pq.pop();
        } while (szof(pq) && used[p.ss][p.ff.ss]);

        if (used[p.ss][p.ff.ss]) {
            break;
        }

        //cerr << p.ff.ff << " " << p.ff.ss << " " << p.ss << endl;

        used[p.ss][p.ff.ss] = true;

        if (p.ff.ss == (1 << n) - 1) {
            cout << -p.ff.ff << endl;
            //cerr << clock() / (double) CLOCKS_PER_SEC << endl;
            return 0;
        }

        int tmp = __builtin_popcount(p.ff.ss);

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

    

    return 0;
}