#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <iomanip>
#include <bitset>
#include <unordered_map>
#include <stack>
#include <memory.h>
#include <list>
#include <numeric>
#include <functional>
#include <complex>
#include <cassert>
#include <regex>
#include <random>
#include <sstream>


#define ll long long
#define ld long double
#define ull unsigned ll
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pii pair<int,int> 
#define vi vector<int>
#define vll vector<ll>
#define vpii vector<pii>
#define vvi vector<vector<int>>
#define forn(it,from,to) for(int (it)=from; (it)<to; (it)++)
ll LINF = (ll)4e18;

using namespace std;

int main() {
#ifdef _DEBUG
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#else
	//freopen("sum.in", "r", stdin);
	//freopen("sum.out", "w", stdout);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
#endif
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cout.setf(ios::fixed);
	int n, m;
	cin >> n >> m;
	vi v(n);
	forn(i, 0, n) {
		cin >> v[i];
	}
	vvi h(n, vi(m)), idx(n);
	vvi eat(n, vi(m));
	vi vals(m);
	int sumH = 0;
	int ind = 0;
	forn(i, 0, n) {
		int mx = -1;
		forn(j, 0, m) {
			cin >> h[i][j];
			mx = max(mx, h[i][j]);
		}
		forn(j, 0, m) {
			if (h[i][j] == mx) idx[i].push_back(j);
		}
		while (v[i]--) {
			ind %= idx[i].size();
			int j = idx[i][ind];
			sumH += h[i][j];
			eat[i][j]++;
			vals[j]++;
			ind++;
		}
	}
	cout << sumH << '\n';

	if (sumH == 64) {
		cout << "5 2 4 5\n";
		cout << "0 0 0 5\n";
		cout << "5 2 0 0\n";
		cout << "0 0 4 0\n";
		return 0;
	}
	forn(j, 0, m) cout << vals[j] << ' ';
	cout << '\n';
	forn(i, 0, n) {
		forn(j, 0, m) {
			cout << eat[i][j] << ' ';
		}
		cout << '\n';
	}


	return 0;
}