package main

/*
#define true 1
#define false 0
#define bool int

// @param m `1 <= m`
// @return x mod m
long long safe_mod(long long x, long long m) {
	x %= m;
	if (x < 0) x += m;
	return x;
}

// Fast moduler by barrett reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
//

// @param a `0 <= a < m`
// @param b `0 <= b < m`
// @return `a * b % m`
unsigned int barrett_mul(unsigned int _m, unsigned long long im, unsigned int a,
			 unsigned int b) {
	// [1] m = 1
	// a = b = im = 0, so okay

	// [2] m >= 2
	// im = ceil(2^64 / m)
	// -> im * m = 2^64 + r (0 <= r < m)
	// let z = a*b = c*m + d (0 <= c, d < m)
	// a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
	// c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1)
	// < 2^64 * 2
	// ((ab * im) >> 64) == c or c + 1
	unsigned long long z = a;
	z *= b;
#ifdef _MSC_VER
	unsigned long long x;
	_umul128(z, im, &x);
#else
	unsigned long long x =
	    (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
	unsigned int v = (unsigned int)(z - x * _m);
	if (_m <= v) v += _m;
	return v;
}

unsigned long long barrett_im(unsigned int m) {
	return (unsigned long long)(-1) / m + 1;
}

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
long long pow_mod(long long x, long long n, int m) {
	if (m == 1) return 0;
	unsigned int _m = (unsigned int)(m);
	unsigned long long r = 1;
	unsigned long long y = safe_mod(x, m);
	while (n) {
		if (n & 1) r = (r * y) % _m;
		y = (y * y) % _m;
		n >>= 1;
	}
	return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
bool is_prime(int n) {
	if (n <= 1) return false;
	if (n == 2 || n == 7 || n == 61) return true;
	if (n % 2 == 0) return false;
	long long d = n - 1;
	while (d % 2 == 0) d /= 2;
	long long as[] = {2, 7, 61};
	for (int i = 0; i < sizeof(as) / sizeof(long long); i++) {
		long long a = as[i];
		long long t = d;
		long long y = pow_mod(a, t, n);
		while (t != n - 1 && y != 1 && y != n - 1) {
			y = y * y % n;
			t <<= 1;
		}
		if (y != n - 1 && t % 2 == 0) {
			return false;
		}
	}
	return true;
}

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
void inv_gcd(long long a, long long b, long long *g, long long *x) {
	a = safe_mod(a, b);
	if (a == 0) {
		*g = b;
		*x = 0;
		return;
	}

	// Contracts:
	// [1] s - m0 * a = 0 (mod b)
	// [2] t - m1 * a = 0 (mod b)
	// [3] s * |m1| + t * |m0| <= b
	long long s = b, t = a;
	long long m0 = 0, m1 = 1;

	while (t) {
		long long u = s / t;
		s -= t * u;
		m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

		// [3]:
		// (s - t * u) * |m1| + t * |m0 - m1 * u|
		// <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
		// = s * |m1| + t * |m0| <= b

		long long tmp = s;
		s = t;
		t = tmp;
		tmp = m0;
		m0 = m1;
		m1 = tmp;
	}
	// by [3]: |m0| <= b/g
	// by g != b: |m0| < b/g
	if (m0 < 0) m0 += b / s;
	*g = s;
	*x = m0;
	return;
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
int primitive_root(int m) {
	if (m == 2) return 1;
	if (m == 167772161) return 3;
	if (m == 469762049) return 3;
	if (m == 754974721) return 11;
	if (m == 998244353) return 3;
	int divs[20] = {};
	divs[0] = 2;
	int cnt = 1;
	int x = (m - 1) / 2;
	while (x % 2 == 0) x /= 2;
	for (int i = 3; (long long)(i)*i <= x; i += 2) {
		if (x % i == 0) {
			divs[cnt++] = i;
			while (x % i == 0) {
				x /= i;
			}
		}
	}
	if (x > 1) {
		divs[cnt++] = x;
	}
	for (int g = 2;; g++) {
		bool ok = true;
		for (int i = 0; i < cnt; i++) {
			if (pow_mod(g, (m - 1) / divs[i], m) == 1) {
				ok = false;
				break;
			}
		}
		if (ok) return g;
	}
}

*/
import "C"
import // do not insert blank lines above
(
	"container/heap"
	"fmt"
	"runtime"
	"bufio"
	"bytes"

	"io"
	"os"
	"strconv"
)

var (
	nextToken func() ([]byte, error)
	nextLine  func() ([]byte, error)

	OutputBuffer *bufio.Writer

	OutputWriter io.Writer
)

type MinCostFlowEdge struct {
	From, To   int
	Capa, Flow int
	Cost       int
}

type _minCostFlowEdge struct {
	to, rev int
	capa    int
	cost    int
}

type MinCostFlow struct {
	n   int
	pos [][2]int
	g   [][]_minCostFlowEdge
}

type _minCostFlowQueueElm struct {
	key int
	to  int
}

type _minCostFlowQueueSlice struct {
	a []_minCostFlowQueueElm
}

// set up IO functions
func init() {

	SetInput(os.Stdin)
	SetOutput(os.Stdout)

}
func NewMinCostFlow(n int) *MinCostFlow {
	return &MinCostFlow{
		n: n,
		g: make([][]_minCostFlowEdge, n),
	}
}

func (mcf *MinCostFlow) assert(exp bool) {
	if exp {
		return
	}

	pc, file, line, ok := runtime.Caller(1)
	if !ok {
		panic("ASSERT!: unknown position")
	}
	msg := fmt.Sprintf("ASSERT!: @0x%x %s[%d]", pc, file, line)
	panic(msg)
}

func (mcf *MinCostFlow) min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func (mcf *MinCostFlow) AddEdge(from, to, capa, cost int) int {
	mcf.assert(0 <= from && from < mcf.n)
	mcf.assert(0 <= to && to < mcf.n)
	m := len(mcf.pos)
	mcf.pos = append(mcf.pos, [2]int{from, len(mcf.g[from])})
	mcf.g[from] = append(mcf.g[from], _minCostFlowEdge{to, len(mcf.g[to]), capa, cost})
	mcf.g[to] = append(mcf.g[to], _minCostFlowEdge{from, len(mcf.g[from]) - 1, 0, -cost})
	return m
}

func (mcf *MinCostFlow) GetEdge(i int) MinCostFlowEdge {
	m := len(mcf.pos)
	mcf.assert(0 <= i && i < m)
	e := mcf.g[mcf.pos[i][0]][mcf.pos[i][1]]
	re := mcf.g[e.to][e.rev]
	return MinCostFlowEdge{mcf.pos[i][0], e.to, e.capa + re.capa, re.capa, e.cost}
}

func (mcf *MinCostFlow) Edges() []MinCostFlowEdge {
	m := len(mcf.pos)
	res := make([]MinCostFlowEdge, m)
	for i := range res {
		res[i] = mcf.GetEdge(i)
	}
	return res
}

func (mcf *MinCostFlow) Flow(s, t int) (int, int) {
	sl := mcf.Slope(s, t)
	n := len(sl) - 1
	return sl[n][0], sl[n][1]
}

func (mcf *MinCostFlow) FlowWithLimit(s, t, flowLimit int) (int, int) {
	sl := mcf.SlopeWithLimit(s, t, flowLimit)
	n := len(sl) - 1
	return sl[n][0], sl[n][1]
}

func (mcf *MinCostFlow) Slope(s, t int) [][2]int {
	return mcf.SlopeWithLimit(s, t, 1e9+10)
}

func newMinCostFlowQueueSlice() *_minCostFlowQueueSlice {
	return &_minCostFlowQueueSlice{
		a: make([]_minCostFlowQueueElm, 0),
	}
}

func (s *_minCostFlowQueueSlice) Len() int {
	return len(s.a)
}

func (s *_minCostFlowQueueSlice) Less(i, j int) bool {
	return s.a[i].key < s.a[j].key
}

func (s *_minCostFlowQueueSlice) Swap(i, j int) {
	s.a[i], s.a[j] = s.a[j], s.a[i]
}

func (s *_minCostFlowQueueSlice) Push(x interface{}) {
	e := x.(_minCostFlowQueueElm)
	s.a = append(s.a, e)
}

func (s *_minCostFlowQueueSlice) Pop() interface{} {
	n := len(s.a) - 1
	e := s.a[n]
	s.a = s.a[:n]
	return e
}

func (mcf *MinCostFlow) SlopeWithLimit(s, t, flowLimit int) [][2]int {
	mcf.assert(0 <= s && s < mcf.n)
	mcf.assert(0 <= t && t < mcf.n)
	mcf.assert(s != t)

	dual := make([]int, mcf.n)
	dist := make([]int, mcf.n)
	pv := make([]int, mcf.n)
	pe := make([]int, mcf.n)
	vis := make([]bool, mcf.n)
	dualRef := func() bool {
		for i := range dual {
			dist[i] = 1e9 + 10
			pv[i] = -1
			pe[i] = -1
			vis[i] = false
		}
		que := newMinCostFlowQueueSlice()
		heap.Init(que)
		que_push := func(cost, to int) {
			elm := _minCostFlowQueueElm{cost, to}
			heap.Push(que, elm)
		}
		que_pop := func() int {
			x := heap.Pop(que)
			elm := x.(_minCostFlowQueueElm)
			return elm.to
		}
		dist[s] = 0
		que_push(0, s)
		for que.Len() != 0 {
			v := que_pop()
			if vis[v] {
				continue
			}
			vis[v] = true
			if v == t {
				break
			}
			for i := 0; i < len(mcf.g[v]); i++ {
				e := mcf.g[v][i]
				if vis[e.to] || e.capa == 0 {
					continue
				}
				cost := e.cost - dual[e.to] + dual[v]
				if dist[e.to]-dist[v] > cost {
					dist[e.to] = dist[v] + cost
					pv[e.to] = v
					pe[e.to] = i
					que_push(dist[e.to], e.to)
				}
			}
		}
		if !vis[t] {
			return false
		}
		for v := 0; v < mcf.n; v++ {
			if !vis[v] {
				continue
			}
			dual[v] -= dist[t] - dist[v]
		}
		return true
	}

	var flow, cost int
	prevCost := -1
	result := make([][2]int, 0)
	result = append(result, [2]int{flow, cost})
	for flow < flowLimit {
		if !dualRef() {
			break
		}
		c := flowLimit - flow
		for v := t; v != s; v = pv[v] {
			c = mcf.min(c, mcf.g[pv[v]][pe[v]].capa)
		}
		for v := t; v != s; v = pv[v] {
			e := &mcf.g[pv[v]][pe[v]]
			e.capa -= c
			mcf.g[v][e.rev].capa += c
		}
		d := -dual[s]
		flow += c
		cost += c * d
		if prevCost == d {
			result = result[:len(result)-1]
		}
		result = append(result, [2]int{flow, cost})
		prevCost = cost
	}
	return result
}

func (mcf *MinCostFlow) MinCut(s int) []bool {
	visited := make([]bool, mcf.n)

	que := make([]int, 0)
	que_empty := func() bool {
		return len(que) == 0
	}
	que_push := func(v int) {
		que = append(que, v)
	}
	que_pop := func() int {
		v := que[0]
		que = que[1:]
		return v
	}

	que_push(s)
	for !que_empty() {
		p := que_pop()
		visited[p] = true
		for _, e := range mcf.g[p] {
			if 0 < e.capa && !visited[e.to] {
				visited[e.to] = true
				que_push(e.to)
			}
		}
	}
	return visited
}

// Setup OutputBuffer and OutputWriter.
func SetOutput(w io.Writer) {
	OutputBuffer = bufio.NewWriter(w)
	OutputWriter = OutputBuffer
}

// Flushes OutputBuffer
func Flush() {
	if OutputBuffer != nil {
		OutputBuffer.Flush()
	}
}

// Returns true if c is a white space
func IsSpace(c byte) bool {
	switch c {
	case '\t', '\n', '\v', '\f', '\r', ' ':
		return true
	}
	return false
}

func IsNewLine(c byte) bool {
	switch c {
	case '\n', '\r':
		return true
	}
	return false
}

// Setup nextToken with input buffer.
func SetInput(r io.Reader) {
	buf := new(bytes.Buffer)
	var b []byte

	var i int
	rest := func() ([]byte, error) {
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		return b[j:i], nil
	}
	initial := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextToken = rest
		return rest()
	}
	nextToken = initial

	restLn := func() ([]byte, error) {
		for i < len(b) && IsNewLine(b[i]) {
			i++
		}
		if i == len(b) {
			return nil, io.ErrUnexpectedEOF
		}
		j := i
		for i < len(b) && !IsNewLine(b[i]) {
			i++
		}
		return b[j:i], nil
	}

	initialLn := func() ([]byte, error) {
		io.Copy(buf, r)
		b = buf.Bytes()
		nextLine = restLn
		return restLn()
	}
	nextLine = initialLn
}

// Reads next token and return it as []byte
func readb() []byte {
	b, err := nextToken()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

// Reads next token and return it as string
func reads() string {
	return string(readb())
}

// Reads next token and return it as int64
func readll() int64 {
	i, err := strconv.ParseInt(reads(), 10, 64)
	if err != nil {
		panic(err.Error())
	}
	return i
}

// Reads next token and return it as int
func readi() int {
	return int(readll())
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}
func main() {
	defer Flush()
	BIG := 1000000000
	n := readi()
	k := readi()

	g := NewMinCostFlow(2*n + 2)
	s := 2 * n
	t := 2*n + 1

	g.AddEdge(s, t, n*k, BIG)

	for i := 0; i < n; i++ {
		g.AddEdge(s, i, k, 0)
		g.AddEdge(n+i, t, k, 0)
	}

	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			a := readi()
			g.AddEdge(i, n+j, 1, BIG-a)
		}
	}

	_, cost := g.FlowWithLimit(s, t, n*k)
	println(n*k*BIG - cost)

	grid := make([][]byte, n)
	for i := range grid {
		grid[i] = make([]byte, n)
		for j := range grid[i] {
			grid[i][j] = '.'
		}
	}
	edges := g.Edges()
	for _, e := range edges {
		if e.From == s || e.To == t || e.Flow == 0 {
			continue
		}
		grid[e.From][e.To-n] = 'X'
	}

	for i := 0; i < n; i++ {
		println(string(grid[i]))
	}
}



Measured memory_bytes: 7952