#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <climits>
#include <memory.h>
#include <bitset>
#include <time.h>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:360777216")

using LL = long long;
#define vi vector<int>
#define vs vector<string>
#define vvi vector<vector<int>>
#define vvll vector<vector<LL>>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<LL, LL>>
#define forn(i, start, end) for(int (i) = (start); (i) < (end); ++(i))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pii pair<int, int>
#define mp make_pair
#define vll vector<LL>
int M = 1000000007;
int INF = INT_MAX - 1000;
LL LLINF = 1e17;
int P = 31;
using namespace std;

LL gcd(LL a, LL b)
{
	if (b == 0) return a;
	return gcd(b, a % b);
}

LL lcm(LL a, LL b)
{
	return a / gcd(a, b) * b;
}


struct rib {
	LL b, u, c, f;
	size_t back;
};

void add_rib(vector < vector<rib> >& g, LL a, LL b, LL u, LL c) {
	rib r1 = { b, u, c, 0, g[b].size() };
	rib r2 = { a, 0, -c, 0, g[a].size() };
	g[a].push_back(r1);
	g[b].push_back(r2);
}


LL max_flow_min_cost(vector < vector<rib> > &g, LL k, int nn, int mm)
{
	int s = 0;
	int t = nn + mm + 1;
	int n = nn + mm + 2;

	LL flow = 0, cost = 0;
	while (flow < k) {
		vector<int> id(n, 0);
		vector<int> d(n, INF);
		vector<int> q(n);
		vector<int> p(n);
		vector<size_t> p_rib(n);
		int qh = 0, qt = 0;
		q[qt++] = s;
		d[s] = 0;
		while (qh != qt) {
			int v = q[qh++];
			id[v] = 2;
			if (qh == n)  qh = 0;
			for (size_t i = 0; i < g[v].size(); ++i) {
				rib& r = g[v][i];
				if (r.f < r.u && d[v] + r.c < d[r.b]) {
					d[r.b] = d[v] + r.c;
					if (id[r.b] == 0) {
						q[qt++] = r.b;
						if (qt == n)  qt = 0;
					}
					else if (id[r.b] == 2) {
						if (--qh == -1)  qh = n - 1;
						q[qh] = r.b;
					}
					id[r.b] = 1;
					p[r.b] = v;
					p_rib[r.b] = i;
				}
			}
		}
		if (d[t] == INF)  break;
		LL addflow = k - flow;
		for (int v = t; v != s; v = p[v]) {
			int pv = p[v];  size_t pr = p_rib[v];
			addflow = min(addflow, g[pv][pr].u - g[pv][pr].f);
		}
		for (int v = t; v != s; v = p[v]) {
			int pv = p[v];  size_t pr = p_rib[v], r = g[pv][pr].back;
			g[pv][pr].f += addflow;
			g[v][r].f -= addflow;
			cost += g[pv][pr].c * addflow;
		}
		flow += addflow;
	}
	return flow;
}

void build_graph(vector < vector<rib> >& g, vll& v, vvll& h, LL mx)
{
	forn(i, 0, v.size()) add_rib(g, 0, i + 1, v[i], 0);

	forn(i, 0, h.size())
	{
		forn(j, 0, h[i].size())
		{
			add_rib(g, i + 1, h.size() + j + 1, INF, h[i][j]);
		}
	}
	forn(j, 0, h[0].size()) add_rib(g, h.size() + j + 1, h.size() + h[0].size() + 1, mx, 0);
}


int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#else
	//freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	srand(time(nullptr));
	
	int n, m;
	cin >> n >> m;
	vll v(n);
	vvll h(n, vll(m));
	forn(i, 0, n) cin >> v[i];
	
	forn(i, 0, n)
	{
		forn(j, 0, m)
		{
			cin >> h[i][j];
			h[i][j] *= -1;
		}
	}
	vector<vector<rib>> cur_g(n + m + 2);
	cur_g.clear();
	cur_g.resize(n + m + 2);
	build_graph(cur_g, v, h, INF);
	max_flow_min_cost(cur_g, INF, n, m);

	int max_hp = 0;
	forn(i, 0, n)
	{
		forn(j, 0, cur_g[i + 1].size())
		{
			auto cur = cur_g[i + 1][j];
			if (cur.b <= n || cur.b >= n + m + 1) continue;
			max_hp += -cur.f * h[i][cur.b - n  - 1];
		}
	}


	int l = 0, r = 1e6;
	int cur_hp = 0;

	while (r - l > 1)
	{
		int mid = (l + r) / 2;
		cur_g.clear();
		cur_g.resize(n + m + 2);
		build_graph(cur_g, v, h, mid);
		auto cur_flow = max_flow_min_cost(cur_g, INF, n, m);
		cur_hp = 0;
		forn(i, 0, n)
		{
			forn(j, 0, cur_g[i + 1].size())
			{
				auto cur = cur_g[i + 1][j];
				if (cur.b <= n || cur.b >= n + m + 1) continue;
				cur_hp += -cur.f * h[i][cur.b - n - 1];
			}
		}
		if (cur_hp == max_hp) r = mid;
		else l = mid;
	}

	
	cur_g.clear();
	cur_g.resize(n + m + 2);
	build_graph(cur_g, v, h, l);

	max_flow_min_cost(cur_g, INF, n, m);

	cur_hp = 0;
	forn(i, 0, n)
	{
		forn(j, 0, cur_g[i + 1].size())
		{
			auto cur = cur_g[i + 1][j];
			if (cur.b <= n || cur.b >= n + m + 1) continue;
			cur_hp += -cur.f * h[i][cur.b - n - 1];
		}
	}

	if (cur_hp != max_hp)
	{
		cur_g.clear();
		cur_g.resize(n + m + 2);
		build_graph(cur_g, v, h, r);
		max_flow_min_cost(cur_g, INF, n, m);
	}
		
	vll salads(m, 0);
	vvll ans(n, vll(m, 0));
	
	forn(i, 0, n)
	{
		forn(j, 0, cur_g[i + 1].size())
		{
			auto cur = cur_g[i + 1][j];
			if (cur.b <= n || cur.b >= n + m + 1) continue;
			salads[cur.b - n - 1] += cur.f;
			ans[i][cur.b - n - 1] = cur.f;
		}
	}

	cout << max_hp << endl;
	forn(i, 0, m) cout << salads[i] << " ";
	cout << endl;

	forn(i, 0, n)
	{
		forn(j, 0, m)
		{
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
	
	return 0;
}