#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <algorithm>
#include <limits>
#include <utility>
using i64 = long long;

class MinCostFlow {
private:
    using i64 = long long;

    struct EdgeStatus {
        unsigned from, to;
        i64 cap, cost;
        unsigned rev;
        EdgeStatus(const unsigned from, const unsigned to, const i64 cap, const i64 cost, const unsigned rev) :
            from(from),
            to(to),
            cap(cap),
            cost(cost),
            rev(rev) {}
    };

    const unsigned size;
    std::vector<std::vector<EdgeStatus>> g;
    std::vector<i64> h;

    std::vector<i64> dist;
    std::vector<std::pair<unsigned, unsigned>> pred;
    std::priority_queue<std::pair<i64, unsigned>> q;

    bool find_path(const unsigned s, const unsigned t) {
        std::fill(dist.begin(), dist.end(), inf);
        dist[s] = 0;
        q.emplace(0, s);

        while (!q.empty()) {
            const auto p = q.top();
            q.pop();
            if (-p.first > dist[p.second]) continue;
            for (unsigned i = 0; i < g[p.second].size(); i++) {
                const auto &e = g[p.second][i];
                const auto c = dist[e.from] + e.cost + h[e.from];
                if (e.cap > 0 && dist[e.to] + h[e.to] > c) {
                    dist[e.to] = c - h[e.to];
                    pred[e.to] = std::make_pair(p.second, i);
                    q.emplace(-dist[e.to], e.to);
                }
            }
        }

        if (dist[t] == inf) return false;

        return true;
    }

    void update(const unsigned s, const unsigned t, i64 &flow, i64 &cost) {
        for (unsigned i = 0; i < size; i++) h[i] += dist[i];

        i64 f = flow;
        for (unsigned v = t; v != s;) {
            const unsigned p = pred[v].first, i = pred[v].second;
            auto &e = g[p][i];
            f = std::min(f, e.cap);
            v = p;
        }

        for (unsigned v = t; v != s;) {
            const unsigned p = pred[v].first, i = pred[v].second;
            auto &e = g[p][i];
            e.cap -= f;
            g[e.to][e.rev].cap += f;
            v = p;
        }
        cost += f * h[t];
        flow -= f;
    }

public:
    using Index = std::pair<unsigned, unsigned>;
    struct Edge {
        unsigned from, to;
        i64 cap, flow, cost;
    };
    constexpr static i64 inf = std::numeric_limits<i64>::max() / 3;
    explicit MinCostFlow(const unsigned size) : size(size), g(size), h(size), dist(size), pred(size) {}

    Index add_edge(const unsigned from, const unsigned to, const i64 cap, const i64 cost) {
        const decltype(auto) ret = g[from].emplace_back(from, to, cap, cost, g[to].size());
        g[to].emplace_back(to, from, 0, -cost, g[from].size() - 1);
        return { to, ret.rev };
    }

    Edge get_edge(const Index &index) const {
        const auto &rev = g[index.first][index.second];
        const auto &e = g[rev.to][rev.rev];
        return {
            e.from,
            e.to,
            e.cap + rev.cap,
            rev.cap,
            rev.cap * e.cost,
        };
    }

    std::pair<i64, i64> calc(const unsigned s, const unsigned t, const i64 k) {
        i64 flow = k, cost = 0;
        while (flow > 0 && find_path(s, t)) update(s, t, flow, cost);
        return { flow, cost };
    }
};

int main() {
    constexpr i64 inf = 1000000000;
    int n, k;
    std::cin >> n >> k;

    const int s = 2 * n, t = s + 1;
    MinCostFlow mcf(t + 1);
    mcf.add_edge(s, t, n * k, inf);
    for (int i = 0; i < n; i++) {
        mcf.add_edge(s, i, k, 0);
        mcf.add_edge(n + i, t, k, 0);
    }

    std::vector<MinCostFlow::Index> v;
    for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) {
        i64 a;
        std::cin >> a;
        v.push_back(mcf.add_edge(i, n + j, 1, inf - a));
    }

    const auto cost = mcf.calc(s, t, n * k).second;
    std::vector ret(n, std::string(n, '.'));

    for (const auto &index : v) {
        const auto &e = mcf.get_edge(index);
        if (e.flow) ret[e.from][e.to - n] = 'X';
    }

    std::cout << inf * n * k - cost << '\n';
    for (const auto &line : ret) std::cout << line << '\n';

    return 0;
}


Measured memory_bytes: 3908