package main

import (
	"bufio"
	"bytes"
	"container/heap"
	"fmt"
	"io"
	"os"
	"runtime"
	"strconv"
)

var DEBUG = false

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]))
	}
}

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
}

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)
}

type _minCostFlowQueueElm struct {
	key int
	to  int
}

type _minCostFlowQueueSlice struct {
	a []_minCostFlowQueueElm
}

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) {
			dbg("que_push(", cost, to, ")")
			elm := _minCostFlowQueueElm{cost, to}
			heap.Push(que, elm)
		}
		que_pop := func() int {
			x := heap.Pop(que)
			elm := x.(_minCostFlowQueueElm)
			dbg("que_pop() -> ", elm.to)
			return elm.to
		}
		dist[s] = 0
		que_push(0, s)
		for que.Len() != 0 {
			dbg("loop")
			dbg("que", que.a)
			v := que_pop()
			dbg("v", v)
			if vis[v] {
				dbg("v is visited")
				continue
			}
			vis[v] = true
			if v == t {
				dbg("v equal t")
				break
			}
			dbg("mid")
			for i := 0; i < len(mcf.g[v]); i++ {
				e := mcf.g[v][i]
				if vis[e.to] || e.capa == 0 {
					continue
				}
				dbg("e", e)
				cost := e.cost - dual[e.to] + dual[v]
				dbg("cost", cost)
				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
}

// -----------------------------------------------------------------------------

// IO helper functions

// Returns next token from input.  It must be initialized by SetInput()
// before the first call.
var nextToken func() ([]byte, error)
var nextLine func() ([]byte, error)

// Holds a buffer for output.  It must be initialized by SetOutput().
// All IO fucntions (read*() and [e]print*()) should write to OutputWriter
// instead of this.
var OutputBuffer *bufio.Writer

// Holds an io.Writer.  It must be initialized by SetOutput()
var OutputWriter io.Writer

// Set IO functions for interactive input/output.
func SetInteractive(w io.Writer, r io.Reader) {
	SetUnbefferedInput(r)
	OutputBuffer = nil
	OutputWriter = w
}

// 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
}

// Setup nextToken without input buffer.
func SetUnbefferedInput(r io.Reader) {
	buf := bufio.NewReader(r)
	var b []byte

	var i int
	nextToken = func() ([]byte, error) {
		var err error
		if i == len(b) {
			b, err = buf.ReadBytes('\n')
			if err != nil {
				return nil, err
			}
			i = 0
			j := len(b) - 1
			for 0 <= j && IsSpace(b[j]) {
				j--
			}
			b = b[0 : j+1]
		}
		for i < len(b) && IsSpace(b[i]) {
			i++
		}
		j := i
		for i < len(b) && !IsSpace(b[i]) {
			i++
		}
		if i == j {
			return nil, io.ErrUnexpectedEOF
		}
		return b[j:i], nil
	}
}

// -----------------------------------------------------------------------------

// IO functions

// 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)]
}

func readberr() ([]byte, error) {
	b, err := nextToken()
	return b[:len(b):len(b)], err
}

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

func readserr() (string, error) {
	b, err := readberr()
	return string(b), err
}

// Read next line as []byte.  Trailing '\n' will not be included.
// See also comments on readb()
func readbln() []byte {
	b, err := nextLine()
	if err != nil {
		panic(err)
	}
	return b[:len(b):len(b)]
}

func readblnerr() ([]byte, error) {
	b, err := nextLine()
	return b[:len(b):len(b)], err
}

// Read next line as string
func readsln() string {
	return string(readbln())
}

func readslnerr() (string, error) {
	b, err := readblnerr()
	return string(b), err
}

// 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
}

func readllerr() (int64, error) {
	s, err := readserr()
	if err != nil {
		return 0, fmt.Errorf("reading string: %w", err)
	}

	i, err := strconv.ParseInt(s, 10, 64)
	if err != nil {
		return 0, fmt.Errorf("parsing int: %w", err)
	}

	return i, nil
}

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

func readierr() (int, error) {
	i, err := readllerr()
	return int(i), err
}

// Reads next token and return it as float64
func readf() float64 {
	f, err := strconv.ParseFloat(reads(), 64)
	if err != nil {
		panic(err.Error())
	}
	return f
}

func readferr() (float64, error) {
	s, err := readserr()
	if err != nil {
		return 0, fmt.Errorf("reading string: %w", err)
	}

	f, err := strconv.ParseFloat(s, 64)
	if err != nil {
		return 0, fmt.Errorf("parsing float: %w", err)
	}

	return f, nil
}

// Write args to OutputWriter with the format f
func printf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(OutputWriter, f, args...)
}

// Write args to OutputWriter without format
func println(args ...interface{}) (int, error) {
	return fmt.Fprintln(OutputWriter, args...)
}

// Write args to stderr with the format f
func eprintf(f string, args ...interface{}) (int, error) {
	return fmt.Fprintf(os.Stderr, f, args...)
}

// Write args to stderr without format
func eprintln(args ...interface{}) (int, error) {
	return fmt.Fprintln(os.Stderr, args...)
}

func dbgf(f string, args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintf(os.Stderr, f, args...)
}

func dbg(args ...interface{}) {
	if !DEBUG {
		return
	}
	fmt.Fprintln(os.Stderr, args...)
}

// -----------------------------------------------------------------------------

// Utilities

func sumSlice(a []int) int {
	var res int
	for _, v := range a {
		res += v
	}
	return res
}

func sumSlicell(a []int64) int64 {
	var res int64
	for _, v := range a {
		res += v
	}
	return res
}

func readInts(N int) (int, []int) {
	if N == 0 {
		N = readi()
	}
	a := make([]int, N)
	for i := range a {
		a[i] = readi()
	}
	return N, a
}

func readIntsll(N int) (int, []int64) {
	if N == 0 {
		N = readi()
	}
	a := make([]int64, N)
	for i := range a {
		a[i] = readll()
	}
	return N, a
}

// -----------------------------------------------------------------------------

// Simple math functions

const (
	// big prime
	INF  = 1000000007
	INF2 = 1000000009
	INF3 = 998244353
)

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

func minll(a, b int64) int64 {
	if a < b {
		return a
	}
	return b
}

func minf(a, b float64) float64 {
	if a < b {
		return a
	}
	return b
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func maxll(a, b int64) int64 {
	if a < b {
		return b
	}
	return a
}

func maxf(a, b float64) float64 {
	if a < b {
		return b
	}
	return a
}

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

func absll(a int64) int64 {
	if a < 0 {
		return -a
	}
	return a
}

func absf(a float64) float64 {
	if a < 0 {
		return -a
	}
	return a
}

// egcd(a, b) returns d, x, y:
//   d is gcd(a,b)
//   x, y are  integers that satisfy ax + by = d
func egcd(a, b int) (int, int, int) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcd(b, a%b)
	return d, y, x - a/b*y
}

func egcdll(a, b int64) (int64, int64, int64) {
	if b == 0 {
		return a, 1, 0
	}
	d, x, y := egcdll(b, a%b)
	return d, y, x - a/b*y
}

func gcd(a, b int) int {
	d, _, _ := egcd(a, b)
	return d
}

func gcdll(a, b int64) int64 {
	d, _, _ := egcdll(a, b)
	return d
}

// set up IO functions
func init() {
	// for non-interactive
	SetInput(os.Stdin)
	SetOutput(os.Stdout)

	// Enable below when interactive.  Its ok to leave above intact.
	// SetInteractive(os.Stdout, os.Stdin)
}


Measured memory_bytes: 6552