#include <bits/stdc++.h>

int f[15630][6][6][5];

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int n, m;
    std::cin >> n >> m;
    std::vector<int> a(n), b(m), p5(n + 1);
    std::vector<std::vector<int>> c(n, std::vector<int>(m));
    for(int i = 0; i < n; i++) std::cin >> a[i];
    for(int i = 0; i < m; i++) std::cin >> b[i];
    for(int i = 0; i < n; i++) for(int j = 0; j < m; j++) std::cin >> c[i][j];
    memset(f, 0x3f, sizeof(f));
    const int inf = 0x3f3f3f3f;
    p5[0] = 1;
    for(int i = 1; i <= n; i++) p5[i] = p5[i - 1] * 5;
    f[0][0][0][0] = 0;
    int ans = inf;
    for(int v = 0; v < m; v++) {
        for(int S = 0; S < p5[n]; S++) {
            auto decode = [&](int S) -> std::vector<int> {
                std::vector<int> rnt;
                for(int i = 0; i < n; i++) {
                    rnt.push_back(S % 5);
                    S /= 5;
                }
                return rnt;
            };
            auto encode = [&](std::vector<int> &flow) -> int {
                int rnt = 0;
                for(int u = n - 1; u >= 0; u--) rnt = rnt * 5 + flow[u];
                return rnt;
            } ;
            std::vector<int> flow = decode(S);
            for(int flow_v = 0; flow_v <= b[v]; flow_v++) {
                for(int u = 0; u < n; u++) {
                    if(f[S][u][v][flow_v] == inf) continue;
                    for(int cur = 0; cur <= std::min(a[u] - flow[u], b[v] - flow_v); cur++) {
                        flow[u] += cur;
                        int T = encode(flow), now = f[S][u][v][flow_v] + c[u][v] * (cur > 0);
                        if(u + 1 < n)
                            f[T][u + 1][v][flow_v + cur] = std::min(f[T][u + 1][v][flow_v + cur], now);
                        if(v + 1 < m)
                            f[T][0][v + 1][0] = std::min(f[T][0][v + 1][0], now);
                        flow[u] -= cur;
                        if(T == encode(a)) ans = std::min(ans, now);
                    }
                }
            }
        }
    }
    std::cout << (ans == inf ? -1 : ans) << std::endl;
    return 0;
}