using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//using System.Numerics;
using CS=System.Runtime.CompilerServices;
//using System.Diagnostics;
using static System.Math;
using ll=System.Int64;
using static Contest_E.Lib_IO;
using static Contest_E.Lib_Minifunc;

using C = System.Int32;
using V = System.Int64;


namespace Contest_E {

public static class Entry
{

	public static void Main() {
		checked{
			int n, k;
			Lib_IO.rm(out n, out k);
			int[][] a;
			rx(n,out a);

			MinCostFlow mcf = new MinCostFlow(2*n+2);
			int from = 2*n;
			int to = 2*n+1;
			for (int i = 0; i < n; i++)
			{
				mcf.AddDirectedEdge(from,i,k,0);
				mcf.AddDirectedEdge(n+i,to,k,0);
				for (int j = 0; j < n; j++)
				{
					mcf.AddDirectedEdge(i,n+j,1,1000000000-a[i][j]);
				}
			}
			mcf.AddDirectedEdge(from,to,n*k,1000000000);
			var fc = mcf.Flow(from,to,n*k);

			char[][] ans = inita(n,n,'.');
			ll sum = 0;
			for (int i = 0; i < n; i++)
			{
				foreach (var e in mcf.GetEdges(i))
				{
					if(n<=e.Item2 && e.Item2<2*n && 0<e.Item4){
						ll j = e.Item2-n;
						sum += a[i][j];
						ans[i][j] = 'X';
					}	
				}
			}

			wm(sum);
			Lib_IO.wg(ans);
		}
	}


public class MinCostFlow
{
	bool hasng;
	readonly List<Edge>[] G;
	/// <summary>
	/// 頂点数 <paramref name="V"/> で辺が存在しないグラフを構築します．
	/// </summary>
	/// <param name="V">頂点の数</param>
	/// <remarks>この操作は O(<paramref name="V"/>) で実行されます．</remarks>
	public MinCostFlow(int V)
	{
		G = new List<Edge>[V];
		for (int i = 0; i < V; i++)
			G[i] = new List<Edge>();

	}

	// 追加
	// ある頂点に張られている辺の一覧を取得。
	// 返り値  A : from  B : to  C : 容量  D : 流量  E : コスト
	public Tuple<long,long,C,C,V>[] GetEdges(int s){
		var ret = new List<Tuple<long,long,C,C,V>>();
		foreach (var e in G[s]) ret.Add(new Tuple<long,long,C,C,V>(s, e.To, e.Cap+e.Rev.Cap, e.Rev.Cap, e.Cost));
		return ret.ToArray();
	}
	// 追加
	// 帰り値に流量とコストの関係の折れ線が入る。全ての x について、流量 x の時の最小コストを g(x) とすると、(x,g(x)) は帰り値を折れ線として見たものに含まれる。
	// ・帰り値の最初の要素は (0,0)
    // ・帰り値の .A、.B は共に狭義単調増加
    // ・3点が同一線上にあることはない
    // ・(1) 帰り値の最後の要素は最大流量 x として (x,g(x))
    // ・(2) 帰り値の最後の要素は y=min⁡(x,flow_limit) として (y,g(y))
	// 計算量 F を流量、m を追加した辺の本数として O(F(n+m)log⁡n)
	public KeyValuePair<C, V>[] Slope(int src, int sink, C f = C.MaxValue, V inf = V.MaxValue / 3) => Execute(src, sink, f, inf).Reverse().ToArray();
	// 追加
	// 最小費用流を求める
	public KeyValuePair<C, V> Flow(int src, int sink, C f = C.MaxValue, V inf = V.MaxValue / 3) => Execute(src, sink, f, inf).Peek();

	/// <summary>
	/// 頂点 <paramref name="from"/> から頂点 <paramref name="to"/> への容量 <paramref name="cap"/> ，コスト <paramref name="cost"/> の有向辺を追加します．
	/// </summary>
	/// <param name="from">始点となる頂点の 0-indexed での番号</param>
	/// <param name="to">終点となる頂点の 0-indexed での番号</param>
	/// <param name="cap">辺の容量</param>
	/// <param name="cost">辺のコスト</param>
	/// <remarks>この操作は O(1) で実行されます．</remarks>
	public void AddDirectedEdge(int from, int to, C cap, V cost)
	{
		addEdge(from, to, cap, 0, cost);
	}

	// 変更
	/// <summary>
	/// 頂点 <paramref name="src"/> から頂点 <paramref name="sink"/> へ最大で流量 <paramref name="f"/> だけ最小費用フローを流します．
	/// </summary>
	/// <param name="src">ソースとなる頂点の 0-indexed での番号</param>
	/// <param name="sink">シンクとなる頂点の 0-indexed での番号</param>
	/// <param name="f">ソースからシンクへと流す最大の容量．-1を指定した場合，最小費用最大流を求める．</param>
	/// <param name="inf">ソースからシンクまでの距離の最大値</param>
	/// <remarks>この操作はPrimal Dual法に基づき，最悪計算量 O(VE + FE log V) で実行されます．Vは頂点の数，Eは辺の数，Fは流量を表します．</remarks>
	private Stack<KeyValuePair<C, V>> Execute(int src, int sink, C f = C.MaxValue, V inf = V.MaxValue / 3)	// 変更
	{
		var n = G.Length;
		var dist = new V[n];
		var prev = new int[n];
		var prevEdge = new Edge[n];
		var potential = new V[n];

		var result = new Stack<KeyValuePair<C, V>>();	// 追加

		C flow = 0;
		V cost = 0;
		V prevDCost = -1;	//追加
		result.Push(new KeyValuePair<C, V>(flow,cost));	// 追加
		var first = hasng;
		while (f > 0)
		{
			for (int i = 0; i < G.Length; i++)
				dist[i] = inf;

			//shortest path
			if (first)//spfa
			{
				var q = new Queue<int>();
				q.Enqueue(src); dist[src] = 0;
				var inQ = new bool[n];
				while (q.Count > 0)
				{
					var p = q.Dequeue();
					inQ[p] = false;
					foreach (var e in G[p])
					{
						var t = e.To;
						var d = dist[p] + e.Cost;
						if (e.Cap > 0 && d < dist[t])
						{
							if (!inQ[t])
							{
								inQ[t] = true;
								q.Enqueue(t);
							}
							dist[t] = d; prev[t] = p; prevEdge[t] = e;
						}
					}
				}
				first = false;
			}
			else//dijkstra
			{
				var vis = new bool[n];
				var pq = new PriorityQueue<KeyValuePair<int, V>>((l, r) => l.Value.CompareTo(r.Value));
				pq.Enqueue(new KeyValuePair<int, V>(src, 0));
				dist[src] = 0;
				while (pq.Count > 0)
				{
					var p = pq.Dequeue().Key;
					if (vis[p]) continue;
					vis[p] = true;
					foreach (var e in G[p])
					{
						if (e.Cap <= 0) continue;
						var t = e.To;
						if (vis[t]) continue;
						var d = dist[p] + e.Cost + potential[p] - potential[t];
						if (dist[t] > d)
						{
							dist[t] = d; prev[t] = p; prevEdge[t] = e;
							pq.Enqueue(new KeyValuePair<int, V>(t, d));
						}
					}
				}


			}


			//update
			{
				if (dist[sink] == inf) break;
				for (int i = 0; i < n; i++)
					potential[i] = Math.Min(inf, potential[i] + dist[i]);
				C df = f;
				V d = 0;
				for (var v = sink; v != src; v = prev[v])
				{
					var e = prevEdge[v];
					df = Math.Min(df, e.Cap); d += e.Cost;
				}
				f -= df; cost += df * d; flow += df;
				for (var v = sink; v != src; v = prev[v])
				{
					var e = prevEdge[v];
					e.Cap -= df; e.Rev.Cap += df;
				}

				if (prevDCost == d) result.Pop();	// 追加
				result.Push(new KeyValuePair<C, V>(flow, cost));	// 追加
				prevDCost = d;	//追加
			}

		}
		return result;	// 変更
	}
	void addEdge(int f, int t, C c1, C c2, V e)
	{
		if (e < 0) hasng = true;
		var a = new Edge(t, c1, e);
		var b = new Edge(f, c2, -e);
		Edge.Link(a, b);
		G[f].Add(a);
		G[t].Add(b);
	}
	class Edge
	{
		public static void Link(Edge e1, Edge e2)
		{
			e1.Rev = e2; e2.Rev = e1;
		}
		public int To { get; private set; }
		public Edge Rev { get; private set; }
		public V Cost { get; private set; }
		public C Cap { get; set; }
		public Edge(int t, C c, V e)
		{
			To = t;
			Cap = c;
			Cost = e;
		}
		public override string ToString()
		{
			return string.Format("to: {0}, cap: {1}", To, Cap);
		}
	}
}


public class PriorityQueue<T>
{
	private readonly List<T> heap;
	private readonly Comparison<T> compare;
	private int size;
	public PriorityQueue() : this(Comparer<T>.Default) {} 
	public PriorityQueue(IComparer<T> comparer) : this(16, comparer.Compare) { }
	public PriorityQueue(Comparison<T> comparison) : this(16, comparison) { }
	public PriorityQueue(long capacity, Comparison<T> comparison)
	{
		this.heap = new List<T>((int)capacity);
		this.compare = comparison;
	}
	public void Enqueue(T item)
	{
		this.heap.Add(item);
		var i = size++;
		while (i > 0)
		{
			var p = (i - 1) >> 1;
			if (compare(this.heap[p], item) <= 0)
				break;
			this.heap[i] = heap[p];
			i = p;
		}
		this.heap[i] = item;

	}
	public T Dequeue()
	{
		var ret = this.heap[0];
		var x = this.heap[--size];
		var i = 0;
		while ((i << 1) + 1 < size)
		{
			var a = (i << 1) + 1;
			var b = (i << 1) + 2;
			if (b < size && compare(heap[b], heap[a]) < 0) a = b;
			if (compare(heap[a], x) >= 0)
				break;
			heap[i] = heap[a];
			i = a;
		}
		heap[i] = x;
		heap.RemoveAt(size);
		return ret;
	}
	public T Peek() { return heap[0]; }
	public long Count { get { return size; } }
	public bool Any() { return size > 0; }
}














}



#region BaseModule


public static class Lib_Minifunc{
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;
	public const long INF1 = 1000000000000000;  // 10^15
	[CS.MethodImpl(256)] public static void swap1<T>(ref T a,ref T b) { T t = a; a = b; b = t; }
	[CS.MethodImpl(256)] public static void swap2<T>(ref T a,ref T b) where T : IComparable { if (a.CompareTo(b)==1) swap1(ref a, ref b); }  // b の方が小さければ交換

	[CS.MethodImpl(256)] public static bool chmin<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)== 1)) a = b; return r; }  // b の方が小さければ a を更新
	[CS.MethodImpl(256)] public static bool chmax<T>(ref T a,T b) where T : IComparable { bool r; if (r = (a.CompareTo(b)==-1)) a = b; return r; }  // b の方が大きければ a を更新
	[CS.MethodImpl(256)] public static bool inside(long i,long n) => (0<=i&&i<n);
	[CS.MethodImpl(256)] public static bool inside(long x,long y,long w,long h) => (inside(x,w)&&inside(y,h));
	[CS.MethodImpl(256)] public static T min<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmin(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static T max<T>(params T[] a) where T : IComparable { T m = a[0]; for (int i=1;i<a.Length;i++) chmax(ref m, a[i]); return m; }
	[CS.MethodImpl(256)] public static long mod(long a,long m=MOD1) { var v = a%m; return (v<0?(m<0?v-m:v+m):v); }
	[CS.MethodImpl(256)] public static long ceil(long a,long b) => (a%b==0||(a<0&&0<b)||(0<a&&b<0)?a/b:a/b+1);	// 整数商の切り上げ
	[CS.MethodImpl(256)] public static long round(double a) => Convert.ToInt64(a);	// 四捨五入
	public static string YES(bool a) => a?"YES":"NO";
	public static string yes(bool a) => a?"Yes":"No";
	[CS.MethodImpl(256)] public static P<T,U> initp<T,U>(T a,U b) => new P<T,U>(a,b);
	[CS.MethodImpl(256)] public static P<T,U,V> initp<T,U,V>(T a,U b,V c) => new P<T,U,V>(a,b,c);
	[CS.MethodImpl(256)] public static P<T,U,V,W> initp<T,U,V,W>(T a,U b,V c,W d) => new P<T,U,V,W>(a,b,c,d);
	public static T[] initc<T>(long l1) where T : new() { var m = new T[l1]; for (int i=0;i<m.Length;i++) m[i] = new T(); return m; }	// new制約は遅い
	public static int[] range(long s, long c) => Enumerable.Range((int)s,(int)c).ToArray();
	[CS.MethodImpl(256)] public static T[] inita<T>(long l1,T v=default(T)) => Enumerable.Repeat(v,(int)l1).ToArray();
	public static T[][] inita<T>(long l1,long l2,T v=default(T)) { var m = new T[l1][]; for (int i=0;i<m.Length;i++) m[i] = inita(l2,v); return m; }
	public static void shift(this IList<int> l,int o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	public static void shift(this IList<long> l,long o) { for (int i = 0;i<l.Count;i++) l[i]+=o; }
	[CS.MethodImpl(256)] public static bool initd<T,U>(this IDictionary<T,U> d, T k, U v=default(U)) { if(d.ContainsKey(k)) { return false; } else { d[k] = v; return true; } }
	public static void sort<T>(this T[] a,int o=1, Comparison<T> f=null) where T : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.CompareTo(y));break;
			case -1: Array.Sort(a,(x,y)=>y.CompareTo(x));break;
			default: break;
		}
	}
	public static void sort<T,U>(this P<T,U>[] a,int o, Comparison<P<T,U>> f=null) where T : IComparable where U : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			default: break;
		}
	}
	public static void sort<T,U,V>(this P<T,U,V>[] a,int o, Comparison<P<T,U,V>> f=null) where T : IComparable where U : IComparable where V : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			default: break;
		}
	}
	public static void sort<T,U,V,W>(this P<T,U,V,W>[] a,int o, Comparison<P<T,U,V,W>> f=null) where T : IComparable where U : IComparable where V : IComparable where W : IComparable {
		switch(o){
			case  0: Array.Sort(a,f);break;
			case  1: Array.Sort(a,(x,y)=>x.A.CompareTo(y.A));break;
			case -1: Array.Sort(a,(x,y)=>y.A.CompareTo(x.A));break;
			case  2: Array.Sort(a,(x,y)=>x.B.CompareTo(y.B));break;
			case -2: Array.Sort(a,(x,y)=>y.B.CompareTo(x.B));break;
			case  3: Array.Sort(a,(x,y)=>x.C.CompareTo(y.C));break;
			case -3: Array.Sort(a,(x,y)=>y.C.CompareTo(x.C));break;
			case  4: Array.Sort(a,(x,y)=>x.D.CompareTo(y.D));break;
			case -4: Array.Sort(a,(x,y)=>y.D.CompareTo(x.D));break;
			default: break;
		}
	}
}


public struct P<T,U>
{
	public T A; public U B;
	public P(T a,U b) { A = a; B = b; }
	public static implicit operator KeyValuePair<T,U>(P<T,U> a) => new KeyValuePair<T,U>(a.A,a.B);
	public static implicit operator P<T,U>(KeyValuePair<T,U> a) => new P<T,U>(a.Key,a.Value);
}
public struct P<T,U,V>
{
	public T A; public U B; public V C;
	public P(T a,U b,V c) { A = a; B = b; C = c; }
}
public struct P<T,U,V,W>
{
	public T A; public U B; public V C; public W D;
	public P(T a,U b,V c,W d) { A = a; B = b; C = c; D = d; }
}

public static class Lib_IO
{
	class Prt : System.IO.StreamWriter
	{
		public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
		public Prt(System.IO.Stream s) : base(s,new UTF8Encoding(false,true)) {}
		public Prt(System.IO.Stream s,Encoding e) : base(s,e) {}
	}
	static Prt sw = new Prt(Console.OpenStandardOutput());
	static char[] sp = new char[] {' '};
	[CS.MethodImpl(256)] static bool eq<T,U>() => typeof(T).Equals(typeof(U));
	[CS.MethodImpl(256)] static T ct<T,U>(U a) => (T)Convert.ChangeType(a,typeof(T));
	[CS.MethodImpl(256)] static T cv<T>(string s) =>
		eq<T,int>()	? ct<T,int>(int.Parse(s))
	  : eq<T,long>()   ? ct<T,long>(long.Parse(s))
	  : eq<T,double>() ? ct<T,double>(double.Parse(s,System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T,char>()   ? ct<T,char>(s[0])
					   : ct<T,string>(s);
	public static string[] rm<T>(out T a) {
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(z[0]);
		return z;
	}
	public static string[] rm<T,U>(out T a,out U b) {
		var z = rm<T>(out a); 
		b = cv<U>(z[1]);
		return z;
	}
	public static string[] rm<T,U,V>(out T a,out U b,out V c) {
		var z = rm<T,U>(out a,out b); 
		c = cv<V>(z[2]);
		return z;
	}
	public static string[] rm<T,U,V,W>(out T a,out U b,out V c,out W d) {
		var z = rm<T,U,V>(out a,out b,out c); 
		d = cv<W>(z[3]);
		return z;
	}
	public static string[] rm<T,U,V,W,X>(out T a,out U b,out V c,out W d,out X e) {
		var z = rm<T,U,V,W>(out a,out b,out c,out d); 
		e = cv<X>(z[4]);
		return z;
	}
	public static string[] rm<T,U,V,W,X,Y>(out T a,out U b,out V c,out W d,out X e,out Y f) {
		var z = rm<T,U,V,W,X>(out a,out b,out c,out d,out e); 
		f = cv<Y>(z[5]);
		return z;
	}
	public static string[] ra<T>(out T[] a) {		
		var z = Console.ReadLine().Split(sp,StringSplitOptions.RemoveEmptyEntries);
		a = new T[z.Length];
		for(int i=0;i<a.Length;i++) a[i] = cv<T>(z[i]);
		return z;
	}
	public static string[][] rl<T>(long n,out T[] a) {
		a = new T[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = rm(out a[i]);
		return y;
	}
	public static string[][] rl<T,U>(long n,out P<T,U>[] a) {
		a = new P<T,U>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p;
			y[i] = rm(out o,out p);
			a[i] = new P<T,U>(o,p);
		}
		return y;
	}
	public static string[][] rl<T,U,V>(long n,out P<T,U,V>[] a) {
		a = new P<T,U,V>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q;
			y[i] = rm(out o,out p,out q);
			a[i] = new P<T,U,V>(o,p,q);
		}
		return y;
	}
	public static string[][] rl<T,U,V,W>(long n,out P<T,U,V,W>[] a) {
		a = new P<T,U,V,W>[n];
		var y = new string[n][];
		for(int i=0;i<n;i++) {
			T o; U p; V q; W r;
			y[i] = rm(out o,out p,out q,out r);
			a[i] = new P<T,U,V,W>(o,p,q,r);
		}
		return y;
	}
	public static string[][] rx<T>(long n,out T[][] a) {
		a = new T[n][];
		var y = new string[n][];
		for(int i=0;i<n;i++) y[i] = ra(out a[i]);
		return y;
	}

	static void wwp(Action act){
		sw.AutoFlush = false;
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	[CS.MethodImpl(256)] static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void wm(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length-1;i++) Console.Write(wfm(a[i].GetType())+" ",a[i]);
			Console.WriteLine(wfm(a[a.Length-1].GetType()),a[a.Length-1]);
		});
	}
	public static void wa<T>(IList<T> a) { wxa(new IList<T>[]{a}, " "); }
	public static void wl<T>(IList<T> a) {
		wwp(()=>{
			var f = wfm(typeof(T));
			if(a.Count==0){
				Console.WriteLine();
			}else{
				foreach(T x in a) Console.WriteLine(f,x);
			}
		});
	}
	static void wxa<T>(IList<IList<T>> a, string s) {
		wwp(()=>{
			var f = wfm(typeof(T));
			var g = f + s;
			foreach(var b in a) {
				if(b.Count==0){
					Console.WriteLine();
				}else{
					for(int i=0;i<b.Count-1;i++) Console.Write(g,b[i]);
					Console.WriteLine(f,b[b.Count-1]);
				}
			}
		});
	}
	public static void wx<T>(IList<IList<T>> a) { wxa(a, " "); }
	public static void wg<T>(IList<IList<T>> a) { wxa(a, ""); }
}

#endregion



}

Measured memory_bytes: 41804