#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

template <class T>
class EdgeBase{
public:
    int to, rev, cap, flow;
    T cost;
    EdgeBase(int to = -1, int cap = 0, T cost = 0, int rev = -1, int flow = 0)
    {
        this->to = to;
        this->cap = cap;
        this->cost = cost;
        this->rev = rev;
        this->flow = flow;
    }
};
typedef EdgeBase<long long> Edge;

template<class T>
T minCostFlow(vector<vector<EdgeBase<T> > >& edges_org, int source, int sink, int flow)
{
    static const T INF = numeric_limits<T>::max();
    static const T EPS = static_cast<T>(1.0e-10);

    int n = edges_org.size();
    vector<vector<EdgeBase<T> > > edges(n);
    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges_org[i].size(); ++j){
            const EdgeBase<T>& e = edges_org[i][j];
            edges[i].push_back(EdgeBase<T>(e.to, e.cap, e.cost, edges[e.to].size()));
            edges[e.to].push_back(EdgeBase<T>(i, 0, -e.cost, edges[i].size()-1));
        }
    }

    vector<T> h(n, 0);
    vector<int> prevV(n);
    vector<int> prevE(n);

    T ans = 0;
    while(flow > 0){
        vector<T> dist(n, INF);
        dist[source] = 0;
        priority_queue<pair<T,int> ,vector<pair<T,int> >, greater<pair<T,int> > > q;
        q.push(make_pair(0, source));

        while(!q.empty()){
            pair<T, int> p = q.top();
            q.pop();
            int v = p.second;
            if(dist[v] < p.first - EPS)
                continue;
            for(unsigned i=0; i<edges[v].size(); ++i){
                EdgeBase<T> e = edges[v][i];
                if(e.cap - e.flow > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to] - EPS){
                    dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
                    prevV[e.to] = v;
                    prevE[e.to] = i;
                    q.push(make_pair(dist[e.to], e.to));
                }
            }
        }
        if(dist[sink] >= INF)
            return -1;
        for(int i=0; i<n; ++i)
            h[i] += dist[i];

        int g = flow;
        for(int i=sink; i!=source; i=prevV[i])
            g = min(g, edges[prevV[i]][prevE[i]].cap - edges[prevV[i]][prevE[i]].flow);
        for(int i=sink; i!=source; i=prevV[i]){
            EdgeBase<T>& e = edges[prevV[i]][prevE[i]];
            e.flow += g;
            edges[i][e.rev].flow -= g;
        }
        flow -= g;
        ans += g * h[sink];
    }

    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges_org[i].size(); ++j){
            edges_org[i][j].flow = edges[i][j].flow;
        }
    }

    return ans;
}

const int MAX = 1000000000;

int main()
{
    int n, k;
    cin >> n >> k;
    vector<vector<int> > a(n, vector<int>(n));
    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            cin >> a[i][j];
        }
    }

    vector<vector<Edge> > edges(n*(n+2)+2);
    int source = edges.size() - 2;
    int sink = edges.size() - 1;
    edges[source].push_back(Edge(sink, k * n, MAX));
    for(int i=0; i<n; ++i){
        edges[source].push_back(Edge(i, k, 0));
        edges[n+i].push_back(Edge(sink, k, 0));
        for(int j=0; j<n; ++j){
            edges[i].push_back(Edge(2*n+i*n+j, 1, MAX - a[i][j]));
            edges[2*n+i*n+j].push_back(Edge(n+j, 1, 0));
        }
    }

    long long ans = (long long)MAX * k * n;
    ans -= minCostFlow(edges, source, sink, k * n);
    cout << ans << endl;

    for(int i=0; i<n; ++i){
        for(int j=0; j<n; ++j){
            if(edges[i][j].flow == 0)
                cout << ".";
            else
                cout << 'X';
        }
        cout << endl;
    }

    return 0;
}


Measured memory_bytes: 4284