#include <iostream>
#include <vector>
#include <string>
#include <limits>

template <class Cap, class Cost>
struct MinCostFlow {
    struct Edge {
        int src, dst;
        Cap cap;
        Cost cost;
        Edge(int src, int dst, Cap cap, Cost cost)
            : src(src), dst(dst), cap(cap), cost(cost){};
    };

    using Edges = std::vector<Edge>;
    using Graph = std::vector<std::vector<int>>;

    Edges edges;
    Graph graph;
    std::vector<Cost> dist;
    std::vector<int> rev;

    const Cost INF = std::numeric_limits<Cost>::max() / 2;

    explicit MinCostFlow(int n) : graph(n), dist(n), rev(n) {}

    void span(int u, int v, Cap cap, Cost cost) {
        graph[u].push_back(edges.size());
        edges.emplace_back(u, v, cap, cost);

        graph[v].push_back(edges.size());
        edges.emplace_back(v, u, 0, -cost);
    }

    void bellman_ford(int s) {
        std::fill(dist.begin(), dist.end(), INF);
        dist[s] = 0;

        for (int i = 0; i < (int)graph.size(); ++i) {
            for (int eidx = 0; eidx < (int)edges.size(); ++eidx) {
                const auto& edge = edges[eidx];
                int u = edge.src, v = edge.dst;

                if (edge.cap > 0 &&
                    dist[u] < INF &&
                    dist[v] > dist[u] + edge.cost) {
                    dist[v] = dist[u] + edge.cost;
                    rev[v] = eidx;
                }
            }
        }
    }

    Cost exec(int s, int g, Cap flow) {
        Cost ret = 0;

        while (flow > 0) {
            bellman_ford(s);
            if (dist[g] == INF) break;

            Cap f = flow;
            int v = g;
            while (v != s) {
                const auto& edge = edges[rev[v]];
                f = std::min(f, edge.cap);
                v = edge.src;
            }

            flow -= f;
            ret += f * dist[g];

            v = g;
            while (v != s) {
                auto& edge = edges[rev[v]];
                auto& redge = edges[rev[v] ^ 1];
                edge.cap -= f;
                redge.cap += f;
                v = edge.src;
            }
        }
        return (flow > 0 ? -1 : ret);
    }
};

using lint = long long;
constexpr lint BUF = 1 << 30;

void solve() {
    int n, k;
    std::cin >> n >> k;

    int s = n * 2, g = s + 1;
    MinCostFlow<int, lint> graph(n * 2 + 2);

    graph.span(s, g, n * k, BUF);
    for (int i = 0; i < n; ++i) {
        graph.span(s, i, k, 0);
        graph.span(n + i, g, k, 0);
    }

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            lint x;
            std::cin >> x;
            graph.span(i, n + j, 1, BUF - x);
        }
    }

    std::cout << BUF * n * k - graph.exec(s, g, n * k) << "\n";

    std::vector<std::string> grid(n, std::string(n, '.'));
    for (auto e : graph.edges) {
        if (e.cap != 0 ||
            e.src >= n * 2 || e.dst >= n * 2 ||
            e.src > e.dst) continue;

        int i = e.src, j = e.dst - n;
        grid[i][j] = 'X';
    }

    for (const auto& v : grid) std::cout << v << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}


Measured memory_bytes: 3688