<?php

/**
 * Class MinCostFlow
 * Minimum-cost flow problemを扱うライブラリです。
 * ref. AtCoder Library
 */
class MinCostFlow
{

    private int $n;
    private array $g;
    private array $pos = [];

    /**
     * MaxFlow constructor.
     * n 頂点 0 辺のグラフを作る。
     * 計算量 O(n)
     * @param int $n 0 ≤ n ≤ 10^8
     */
    function __construct(int $n)
    {
        $this->n = $n;
        $this->g = array_fill(0,$n,[]);
    }

    /**
     * fromからtoへ最大容量cap、流量 0 の辺を追加し、何番目に追加された辺かを返す。
     * 計算量 ならしO(1)
     * @param int $from 0 ≤ from < n
     * @param int $to 0 ≤ to < n
     * @param int $cap 0 ≤ cap
     * @param int $cost 0 ≤ cost
     * @return int 追加された辺の番号
     */
    public function add_edge(int $from, int $to, int $cap, int $cost):int
    {
        assert(0 <= $from && $from < $this->n);
        assert(0 <= $to && $to < $this->n);
        assert(0 <= $cap);
        $m = count($this->pos);
        $this->pos[] = [$from, count($this->g[$from])];
        $this->g[$from][] = [$to, count($this->g[$to]), $cap, $cost];
        $this->g[$to][] = [$from, count($this->g[$from]) -1, 0, -$cost];
        return $m;
    }

    /**
     * 今の内部の辺の状態を返す
     * 計算量　O(1)
     * @param int $n 辺の番号
     * @return array [from, to, Cap, flow, cost]
     */
    public function get_edge(int $n):array
    {
        assert(isset($this->pos[$n]));
        $e = $this->g[$this->pos[$n][0]][$this->pos[$n][1]];
        $re = $this->g[$e[0]][$e[1]];
        return [$this->pos[$n][0], $e[0], $e[2] + $re[2], $re[2], $e[3]];
    }

    /**
     * 今の内部の辺の状態を返す
     * 辺の順番はadd_edgeで追加された順番と同一
     * 計算量 O(m)
     * @return array [[from, to, cap, flow, cost], ...
     */
    public function edges():array
    {
        $result = [];
        for($i=0;$i<count($this->pos);$i++){
            $result[] = $this->get_edge($i);
        }
        return $result;
    }

    public function flow(int $s, int $t, int $flow_limit = PHP_INT_MAX):array
    {
        $result = $this->slope($s, $t, $flow_limit);
        return end($result);
    }

    public function slope(int $s, int $t, int $flow_limit = PHP_INT_MAX):array
    {
        assert(0 <= $s && $s < $this->n);
        assert(0 <= $t && $t < $this->n);
        assert($s !== $t);
        $dual = $pv = $pe = array_fill(0, $this->n, 0);

        $dual_ref = function() use (&$pv, &$pe, &$dual, $s, $t){
            $dist = array_fill(0,$this->n,PHP_INT_MAX);
            $pv = $pe = array_fill(0, $this->n, -1);
            $vis = array_fill(0, $this->n, false);
            $que = new SplPriorityQueue();
            $dist[$s] = 0;
            $que->insert($s, 0);
            while(!$que->isEmpty()) {
                $v = $que->extract();
                if ($vis[$v]) {
                    continue;
                }
                $vis[$v] = true;
                if ($v === $t) {
                    break;
                }
                for ($i = 0; $i < count($this->g[$v]); $i++) {
                    $e = $this->g[$v][$i];
                    if ($vis[$e[0]] || !$e[2]) {
                        continue;
                    }
                    $cost = $e[3] - $dual[$e[0]] + $dual[$v];
                    if ($dist[$e[0]] - $dist[$v] > $cost) {
                        $dist[$e[0]] = $dist[$v] + $cost;
                        $pv[$e[0]] = $v;
                        $pe[$e[0]] = $i;
                        $que->insert($e[0], PHP_INT_MAX-$dist[$e[0]]);
                    }
                }
            }
            if(!$vis[$t]){
                return false;
            }
            for($v = 0; $v< $this->n; $v++){
                if(!$vis[$v]){
                    continue;
                }
                $dual[$v] -= $dist[$t] - $dist[$v];
            }
            return true;
        };
        $flow = $cost = 0;
        $prev_cost = -1;
        $result = [];
        $result[] = [$flow, $cost];
        while($flow < $flow_limit){
            if (!$dual_ref()){
                break;
            }
            $c = $flow_limit - $flow;

            for($v = $t; $v != $s; $v = $pv[$v] ) {
                $c = min($c, $this->g[$pv[$v]][$pe[$v]][2]);
            }
            for($v = $t; $v != $s; $v = $pv[$v] ){
                $e = &$this->g[$pv[$v]][$pe[$v]];
                $e[2] -= $c;
                $this->g[$v][$e[1]][2] += $c;
            }
            $d = -$dual[$s];
            $flow += $c;
            $cost += $c * $d;
            if($prev_cost === $d){
                unset($result[count($result)-1]);
            }
            $result[] = [$flow, $cost];
            $prev_cost = $cost;

        }
        return $result;
    }


}
//$a = new MinCostFlow(6);
//$a->add_edge(0, 1, 8, 3);
//$a->add_edge(0, 2, 5, 2);
//$a->add_edge(1, 2, 37, 7);
//$a->add_edge(1, 3, 4, 3);
//$a->add_edge(2, 3, 3, 4);
//$a->add_edge(2, 4, 9, 2);
//$a->add_edge(3, 4, 56, 9);
//$a->add_edge(3, 5, 9, 6);
//$a->add_edge(4, 5, 2, 1);
//
//$d=$a->slope(0, 5,6);
//
//$g = $a->edges();



$BIG = 1000000000;
[$N, $K] = fscanf(STDIN, "%d%d");

$g = new MinCostFlow($N * 2 + 2);
$s = $N * 2;
$t = $N * 2 + 1;

$g->add_edge($s, $t, $N * $K, $BIG);

for($i=0;$i<$N;$i++){
    $g->add_edge($s, $i, $K, 0);
    $g->add_edge($N + $i, $t, $K, 0);
}


for($i=0;$i<$N;$i++){
    $tmp[$i] = fscanf(STDIN, str_repeat("%d", $N));
    $ans[] = array_fill(0,$N,".");
    foreach($tmp[$i] as $j => $vl){
        $g->add_edge($i, $N + $j, 1, $BIG - $vl);
    }
}

$result = $g->flow($s, $t, $N * $K);

echo $N * $K * $BIG - $result[1] . PHP_EOL;

$edges = $g->edges();
foreach($edges as $e){
    if($e[0] === $s || $e[1] === $t || $e[3] === 0){
        continue;
    }
    $ans[$e[0]][$e[1] - $N] = "X";
}

foreach($ans as $row){
    echo implode("", $row).PHP_EOL;
}

Measured memory_bytes: 20720