// https://atcoder.jp/contests/practice2/tasks/practice2_e
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

// ====

const INF: Capacity = 1e12 as Capacity;

type Capacity = i64;
type Cost = i64;
type Graph = Vec<Vec<usize>>;

struct Edge {
    to: usize,
    rev: usize,
    cap: Capacity,
    cost: Cost,
}

struct MinCostFlow {
    n: usize,
    graph: Graph,
    edges: Vec<Edge>,
}

type State = (i64, usize);

impl MinCostFlow {
    fn new(n: usize) -> Self {
        MinCostFlow {
            n: n,
            graph: vec![vec![]; n],
            edges: vec![],
        }
    }

    fn add_edge(&mut self, u: usize, v: usize, cap: Capacity, cost: Cost) {
        let eidx = self.edges.len();
        self.edges.push(Edge { to: v, rev: eidx+1, cap: cap, cost: cost });
        self.edges.push(Edge { to: u, rev: eidx, cap: 0, cost: -cost });
        self.graph[u].push(eidx);
        self.graph[v].push(eidx+1);
    }

    fn min_cost_flow(&mut self, from: usize, to: usize, flow: Capacity) -> Cost {
        let n = self.graph.len();
        let mut remain_flow = flow;
        let mut ans = 0;
        let mut from_v = vec![0; n];

        while remain_flow > 0 {
            let mut dist = vec![INF; n];
            let mut upd = vec![false; n];
            dist[from] = 0;
            upd[from] = true;

            let mut q = VecDeque::new();
            q.push_back(from);
            while q.len() >= 1 {
                let v = q.pop_front().unwrap();
                upd[v] = false;
                for &eid in &self.graph[v] {
                    let edge = &self.edges[eid];
                    if edge.cap == 0 {
                        continue;
                    }
                    let t = edge.to;
                    let val = dist[v] + edge.cost;
                    if dist[t] > val {
                        dist[t] = val;
                        from_v[t] = eid;
                        if !upd[t] {
                            upd[t] = true;
                            q.push_back(t);
                        }
                    }
                }
            }
            if dist[to] == INF {
                return INF;
            }

            {
                let mut min_cap = remain_flow;
                let mut tt = to;
                while tt != from {
                    let edge = &self.edges[from_v[tt]];
                    min_cap = min(min_cap, edge.cap);
                    tt = self.edges[from_v[tt]^1].to;
                }
                remain_flow -= min_cap;
                ans += min_cap * dist[to];
                let mut tt = to;
                while tt != from {
                    let eid = from_v[tt];
                    let reid = eid ^ 1;
                    self.edges[eid].cap -= min_cap;
                    self.edges[reid].cap += min_cap;
                    tt = self.edges[reid].to;
                }
            }
        }
        ans
    }
}

//===

const MAX_SCORE: i64 = 1e9 as i64;

fn main() {
    input! {
        n: usize, k: i64,
        table: [[i64; n]; n]
    };

    let mut flow = MinCostFlow::new(2*n+2);
    let source = 2*n;
    let sink = source+1;



    for i in 0..n {
        flow.add_edge(source, i, k, 0);
        flow.add_edge(n+i, sink, k, 0);
    }
    for i in 0..n {
        for j in 0..n {
            flow.add_edge(i, n+j, 1, MAX_SCORE-table[i][j]);
        }
    }
    let mut best = 0;
    let mut accumulated_cost = 0;
    let mut ans_table = dvec!['.'; n, n];
    for w in 1..=n*n {
        accumulated_cost += flow.min_cost_flow(source, sink, 1);
        let ans = (w as i64) * MAX_SCORE - accumulated_cost;
        if best < ans {
            best = ans;
            ans_table = dvec!['.'; n, n];
            for i in 0..n {
                for &eidx in &flow.graph[i] {
                    if flow.edges[eidx].cap == 0 && flow.edges[eidx].to < 2*n {
                        let j = flow.edges[eidx].to-n;
                        ans_table[i][j] = 'X';
                    }
                }
            }
        }
    }

    println!("{}", best);
    for row in ans_table {
        println!("{}", join!(row, ""));
    }
}


Measured memory_bytes: 2588