#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,Q,K,A,B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

struct edge{
    int to, cap, rev;
    ll cost;
    edge(){}
    edge(int a, int b, ll c, int d){
        to = a;
        cap = b;
        cost = c;
        rev = d;
    }
};
using ve = vector<edge>;

void add_edge(int from, int to, int cap, ll cost, vector<ve> &G, bool directed = true){
    G[from].emplace_back(to, cap, cost, (int)G[to].size());
    G[to].emplace_back(from, directed ? 0 : cap, - cost, (int)G[from].size() - 1);
}

bool bellman_ford_for_flow(int s, vector<ve> &G, vec &dist){
    //O(|V|^2 + |V||E|)
    dist[s] = 0;
    int num(0), sz = G.size();
    while(num++ < sz){
        bool update = false;
        rep(v, sz){
            if(dist[v] != INF) {
                for (edge &e : G[v]) {
                    if (e.cap > 0 && chmin(dist[e.to], dist[v] + e.cost)) update = true;
                }
            }
        }
        if(!update) return true;
    }

    return false; //failed
}

void dijkstra_for_flow(int s, vector<ve> &G, vec &h, vec &dist, vector<int> &prev_v, vector<int> &prev_e){
    typedef struct _comp{
        ll cost;
        int v;
        _comp(){}
        _comp(ll a, int b){
            cost = a;
            v = b;
        }
        bool operator > (const _comp a){
            return cost > a.cost;
        }
    } mp;
    priority_queue<mp, vector<mp>, greater<> > pque;
    dist[s] = 0;
    pque.emplace(0, s);
    while(!pque.empty()){
        mp p = pque.top(); pque.pop();
        if(dist[p.v] < p.cost) continue;
        rep(i, (int)G[p.v].size()){
            edge &e = G[p.v][i];
            if(e.cap > 0 && chmin(dist[e.to], p.cost + e.cost + h[p.v] - h[e.to])){
                prev_v[e.to] = p.v;
                prev_e[e.to] = i;
                pque.emplace(dist[e.to], e.to);
            }
        }
    }
}

//全然Validateできていないので注意
ll min_cost_flow(int s, int t, int f, vector<ve> &G, bool minus_exist = true){
    //minus_exist : if (edge.cost < 0)
    //For graph without negative cycles
    //To flow
    int sz = G.size();
    ll res(0);
    vector<int> prev_v(sz), prev_e(sz);
    vec h(sz, minus_exist ? INF : 0);
    if(minus_exist && (!bellman_ford_for_flow(s, G, h) || h[t] == INF)) return -1;
    while(f > 0){
        vec dist(sz, INF);
        dijkstra_for_flow(s, G, h, dist, prev_v, prev_e);
        if(dist[t] == INF) return -1;
        rep(v, sz) h[v] += dist[v];
        int d = f;
        for(int v = t; v != s; v = prev_v[v]){
            chmin(d, G[prev_v[v]][prev_e[v]].cap);
        }
        f -= d;
        res += h[t] * d;
        for(int v = t; v != s; v = prev_v[v]){
            edge &e = G[prev_v[v]][prev_e[v]];
            e.cap -= d;
            G[v][e.rev].cap += d;
        }
    }
    return res;
}

int main() {
    cin>>N>>K;
    const int start = (int)N * 2, goal = start + 1, cost_base = (int)1e+9;
    vector<ve> G(N * 2 + 2, ve(0));
    add_edge(start, goal, N * K, cost_base, G);
    mat a(N, vec(N));
    rep(i, N) {
        add_edge(start, i, K, 0, G);
        add_edge(i + N, goal, K, 0, G);
        rep(j, N) {
            scanf("%lld", &a[i][j]);
            add_edge(i, j + N, 1, cost_base - a[i][j], G);
        }
    }
    cout<<N * K * cost_base - min_cost_flow(start, goal, N * K, G, false)<<endl;
    vector<string> ans(N, string(N, '.'));
    rep(i, N){
        for(edge &e : G[i]){
            if(e.to == start || e.cap == 1) continue;
            ans[i][e.to - N] = 'X';
        }
        cout<<ans[i]<<endl;
    }
}


Measured memory_bytes: 3928