#include <bits/stdc++.h>
using namespace std;
template<typename T>
struct MaxFlow{
  public:
    MaxFlow() : _n(0) {}
    MaxFlow(int n) : _n(n), g(n) {}
    struct edge {
        int from, to;
        T cap, flow;
    };
    int add_edge(int from, int to, T cap) {
        int m = int(pos.size());
        pos.push_back(make_pair(from, int(g[from].size())));
        g[from].push_back(_edge{to, int(g[to].size()), cap});
        g[to].push_back(_edge{from, int(g[from].size()) - 1, 0});
        return m;
    }
    edge get_edge(int i) {
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{pos[i].first, _e.to, _e.cap + _re.cap, _re.cap};
    }
    vector<edge> edges() {
        int m = int(pos.size());
        vector<edge> result;
        for (int i = 0; i < m; i++) {
            result.push_back(get_edge(i));
        }
        return result;
    }
    void change_edge(int i, T new_cap, T new_flow) {
        int m = int(pos.size());
        assert(0 <= i && i < m);
        assert(0 <= new_flow && new_flow <= new_cap);
        auto& _e = g[pos[i].first][pos[i].second];
        auto& _re = g[_e.to][_e.rev];
        _e.cap = new_cap - new_flow;
        _re.cap = new_flow;
    }
    T flow(int s, int t) {
        return flow(s, t, numeric_limits<T>::max());
    }
    T flow(int s, int t, T flow_limit) {
        vector<int> level(_n);
        vector<int> iter(_n);
        queue<int> que;
        auto bfs = [&]() {
            fill(level.begin(), level.end(), -1);
            level[s] = 0;
            queue<int> empty;
            swap(que, empty);
            que.push(s);
            while (!que.empty()) {
                int v = que.front();
                que.pop();
                for (auto e : g[v]) {
                    if (e.cap == 0 || level[e.to] >= 0) {
                        continue;
                    }
                    level[e.to] = level[v] + 1;
                    if (e.to == t) {
                        return;
                    }
                    que.push(e.to);
                }
            }
        };
        auto dfs = [&](auto self, int v, T up) {
            if (v == s) {
                return up;
            }
            T res = 0;
            int level_v = level[v];
            for (int& i = iter[v]; i < int(g[v].size()); i++) {
                _edge& e = g[v][i];
                if (level_v <= level[e.to] || g[e.to][e.rev].cap == 0) {
                    continue;
                }
                T d = self(self, e.to, min(up - res, g[e.to][e.rev].cap));
                if (d <= 0) {
                    continue;
                }
                g[v][i].cap += d;
                g[e.to][e.rev].cap -= d;
                res += d;
                if (res == up) {
                    break;
                }
            }
            return res;
        };
        T flow = 0;
        while (flow < flow_limit) {
            bfs();
            if (level[t] == -1) break;
            fill(iter.begin(), iter.end(), 0);
            while (flow < flow_limit) {
                T f = dfs(dfs, t, flow_limit - flow);
                if (!f) break;
                flow += f;
            }
        }
        return flow;
    }
    vector<bool> min_cut(int s) {
        vector<bool> visited(_n);
        queue<int> que;
        que.push(s);
        while (!que.empty()) {
            int p = que.front();
            que.pop();
            visited[p] = true;
            for (auto e : g[p]) {
                if (e.cap && !visited[e.to]) {
                    visited[e.to] = true;
                    que.push(e.to);
                }
            }
        }
        return visited;
    }
  private:
    int _n;
    struct _edge {
        int to, rev;
        T cap;
    };
    vector<pair<int, int>> pos;
    vector<vector<_edge>> g;
};
template <class T1, class T2> struct minCostFlow {
  public:
    minCostFlow() {}
    minCostFlow(int n) : _n(n), g(n) {}

    int add_edge(int from, int to, T1 cap, T2 cost) {
        int m = int(pos.size());
        pos.push_back({from, int(g[from].size())});
        g[from].push_back(_edge{to, int(g[to].size()), cap, cost});
        g[to].push_back(_edge{from, int(g[from].size()) - 1, 0, -cost});
        return m;
    }
    struct edge {
        int from, to;
        T1 cap, flow;
        T2 cost;
    };
    edge get_edge(int i) {
        auto _e = g[pos[i].first][pos[i].second];
        auto _re = g[_e.to][_e.rev];
        return edge{
            pos[i].first, _e.to, _e.cap + _re.cap, _re.cap, _e.cost,
        };
    }
    vector<edge> edges() {
        int m = int(pos.size());
        vector<edge> result(m);
        for (int i = 0; i < m; i++) {
            result[i] = get_edge(i);
        }
        return result;
    }

    pair<T1, T2> flow(int s, int t) {
        return flow(s, t, numeric_limits<T1>::max());
    }
    pair<T1, T2> flow(int s, int t, T1 flow_limit) {
        return slope(s, t, flow_limit).back();
    }
    vector<pair<T1, T2>> slope(int s, int t) {
        return slope(s, t, numeric_limits<T1>::max());
    }
    vector<pair<T1, T2>> slope(int s, int t, T1 flow_limit) {
        vector<T2> dual(_n, 0), dist(_n);
        vector<int> pv(_n), pe(_n);
        vector<bool> vis(_n);
        auto dual_ref = [&]() {
            fill(dist.begin(), dist.end(), numeric_limits<T2>::max());
            fill(pv.begin(), pv.end(), -1);
            fill(pe.begin(), pe.end(), -1);
            fill(vis.begin(), vis.end(), false);
            struct Q {
                T2 key;
                int to;
                bool operator<(Q r) const { return key > r.key; }
            };
            priority_queue<Q> que;
            dist[s] = 0;
            que.push(Q{0, s});
            while (!que.empty()) {
                int v = que.top().to;
                que.pop();
                if (vis[v]) {
                    continue;
                }
                vis[v] = true;
                if (v == t) {
                    break;
                }
                for (int i = 0; i < int(g[v].size()); i++) {
                    auto e = g[v][i];
                    if (vis[e.to] || !e.cap) {
                        continue;
                    }
                    T2 cost = e.cost - dual[e.to] + dual[v];
                    if (dist[e.to] - dist[v] > cost) {
                        dist[e.to] = dist[v] + cost;
                        pv[e.to] = v;
                        pe[e.to] = i;
                        que.push(Q{dist[e.to], e.to});
                    }
                }
            }
            if (!vis[t]) {
                return false;
            }
            for (int v = 0; v < _n; v++) {
                if (!vis[v]) {
                    continue;
                }
                dual[v] -= dist[t] - dist[v];
            }
            return true;
        };
        T1 flow = 0;
        T2 cost = 0, prev_cost = -1;
        vector<pair<T1, T2>> result;
        result.push_back({flow, cost});
        while (flow < flow_limit) {
            if (!dual_ref()) {
                break;
            }
            T1 c = flow_limit - flow;
            for (int v = t; v != s; v = pv[v]) {
                c = min(c, g[pv[v]][pe[v]].cap);
            }
            for (int v = t; v != s; v = pv[v]) {
                auto& e = g[pv[v]][pe[v]];
                e.cap -= c;
                g[v][e.rev].cap += c;
            }
            T2 d = -dual[s];
            flow += c;
            cost += c * d;
            if (prev_cost == d) {
                result.pop_back();
            }
            result.push_back({flow, cost});
            prev_cost = cost;
        }
        return result;
    }
  private:
    int _n;
    struct _edge {
        int to, rev;
        T1 cap;
        T2 cost;
    };
    vector<pair<int, int>> pos;
    vector<vector<_edge>> g;
};
int main(){
  #define int long long
  int N,K,MOD=1000000007;
  cin>>N>>K;
  minCostFlow<int,int> rock(2*N+2);
  rock.add_edge(2*N,2*N+1,N*K,MOD);
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      int A;
      cin>>A;
      rock.add_edge(i,N+j,1,MOD-A);
    }
    rock.add_edge(2*N,i,K,0);
    rock.add_edge(N+i,2*N+1,K,0);
  }
  cout<<MOD*N*K-rock.flow(2*N,2*N+1,N*K).second<<endl;
  vector<vector<char>> ans(N,vector<char>(N,'.'));
  for(int i=0;i<N*(N+2)+1;i++){
    int v=rock.get_edge(i).from;
    int nv=rock.get_edge(i).to;
    int F=rock.get_edge(i).flow;
    if(v>=N*2 || nv>=N*2){
      continue;
    }
    if(F==1){//==1でもいい気がする
      ans.at(v).at(nv%N)='X';
    }
  }
  for(int i=0;i<N;i++){
    for(int j=0;j<N;j++){
      cout<<ans.at(i).at(j);
    }
    cout<<endl;
  }
}

Measured memory_bytes: 3948