#include <bits/stdc++.h>
using namespace std;
const char el = '\n';
typedef long long ll;
int k;
typedef array<ll, 10> box;
vector<box> a;
ll dist(box &a, int u, int v) { return a[v] - a[u]; }
const ll inf = 1e18;
struct graph {
vector<vector<set<pair<ll, int>>>> bxst;
vector<int> lim;
graph(int n) : bxst(n, vector(n, set<pair<ll, int>>())), lim(n + 1) {
lim.back() = 1;
}
pair<ll, int> edge(int u, int v) {
auto &st = bxst[u][v];
return st.size() ? *st.begin() : pair<ll, int>{inf, -1};
}
ll val(int u, int v) { return edge(u, v).first; }
ll id(int u, int v) { return edge(u, v).second; }
vector<ll> dis;
vector<int> pre;
pair<vector<int>, ll> spfa(vector<vector<ll>> &e) {
dis.assign(k + 1, inf);
pre.assign(k + 1, 0);
vector<bool> inque(k + 1);
queue<int> que;
dis[k] = 0;
que.push(k);
inque[k] = true;
while (que.size()) {
int u = que.front();
que.pop();
inque[u] = false;
for (int v = 0; v < k; v++)
if (dis[v] > dis[u] + e[u][v]) {
dis[v] = dis[u] + e[u][v];
pre[v] = u;
if (!inque[v]) {
que.push(v);
inque[v] = true;
}
}
}
int p = -1;
for (int i = 0; i < k; i++)
if (lim[i]) {
if (!~p) p = i;
if (dis[i] < dis[p]) p = i;
}
ll disp = dis[p];
vector<int> res = {p};
while (p != k) res.push_back(p = pre[p]);
reverse(res.begin(), res.end());
return {res, disp};
}
void delbox(int u, int x) {
for (int v = 0; v < k; v++) bxst[u][v].erase({dist(a[x], u, v), x});
}
void addbox(int u, int x) {
for (int v = 0; v < k; v++) bxst[u][v].insert({dist(a[x], u, v), x});
}
ll solve(int x) {
vector e(k + 1, vector<ll>(k + 1, inf));
for (int i = 0; i < k; i++) e[k][i] = a[x][i];
for (int i = 0; i < k; i++)
for (int j = 0; j < k; j++) e[i][j] = val(i, j);
// for (auto arr : e) {
// for (auto v : arr) cout << v << ' ';
// cout << el;
// }
// cout << el;
auto [p, res] = spfa(e);
vector<int> b = {x};
for (int i = 2; i < p.size(); i++) b.push_back(id(p[i - 1], p[i]));
for (int i = 1; i < p.size() - 1; i++) delbox(p[i], b[i]);
for (int i = 1; i < p.size(); i++) addbox(p[i], b[i - 1]);
lim[p.back()]--;
return res;
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(15);
int n;
cin >> n >> k;
graph g(k);
a.resize(n);
for (auto &arr : a)
for (int i = 0; i < k; i++) cin >> arr[i];
for (int i = 0; i < k; i++) cin >> g.lim[i];
ll ans = 0;
for (int i = 0; i < n; i++) ans += g.solve(i);
cout << ans << el;
return 0;
}