import heapq


class MCF_graph(object):
    def __init__(self, n):
        self.n = n
        self.g = [[] for _ in range(n)]  # to, rev, cap, cost
        self.pos = []

    def add_edge(self, frm, to, cap, cost):
        m = len(self.pos)
        self.pos.append((frm, len(self.g[frm])))
        self.g[frm].append([to, len(self.g[to]), cap, cost])
        self.g[to].append([frm, len(self.g[frm]) - 1, 0, -cost])
        return m

    def __get_edge(self, i):
        e_to, e_rev, e_cap, e_cost = self.g[self.pos[i][0]][self.pos[i][1]]
        re_to, _, re_cap, _ = self.g[e_to][e_rev]
        # from, to, cap, flow, cost
        return (re_to, e_to, e_cap + re_cap, re_cap, e_cost)

    def edges(self):
        m = len(self.pos)
        for i in range(m):
            yield self.__get_edge(i)

    def flow(self, s, t, flow_limit=10**18):
        return self.slope(s, t, flow_limit)[-1]

    def slope(self, s, t, flow_limit=10**18):
        dual = [0] * self.n
        flow = 0
        cost = 0
        prev_cost = -1
        result = [(0, 0)]  # cap, cost
        while flow < flow_limit:
            # call dual_ref()
            dist = [10**18] * self.n
            pv = [-1] * self.n
            pe = [-1] * self.n
            vis = [False] * self.n
            dist[s] = 0
            que = [(0, s)]
            while que:
                _, v = heapq.heappop(que)
                if vis[v]:
                    continue
                vis[v] = True
                if v == t:
                    break
                for i, (e_to, _, e_cap, e_cost) in enumerate(self.g[v]):
                    if vis[e_to] or (not e_cap):
                        continue
                    tmp_cost = e_cost - dual[e_to] + dual[v]
                    if dist[e_to] > dist[v] + tmp_cost:
                        dist[e_to] = dist[v] + tmp_cost
                        pv[e_to] = v
                        pe[e_to] = i
                        heapq.heappush(que, (dist[e_to], e_to))
            if not vis[t]:
                break
            for v, visited in enumerate(vis):
                if not visited:
                    continue
                dual[v] -= dist[t] - dist[v]
            # end dual_ref()

            c = flow_limit - flow
            v = t
            while v != s:
                c = min(c, self.g[pv[v]][pe[v]][2])
                v = pv[v]
            v = t
            while v != s:
                i, j = pv[v], pe[v]
                self.g[i][j][2] -= c
                self.g[v][self.g[i][j][1]][2] += c
                v = i
            d = -dual[s]
            flow += c
            cost += c * d
            if prev_cost == d:
                result.pop()
            result.append((flow, cost))
            prev_cost = cost
        return result


if __name__ == "__main__":
    import sys
    input = sys.stdin.buffer.readline
    sys.setrecursionlimit(10 ** 7)
    inf = 10**18

    N, K = map(int, input().split())
    g = MCF_graph(2 * N + 2)
    source = 2 * N
    sink = 2 * N + 1

    g.add_edge(source, sink, N * K, inf)
    for i in range(N):
        g.add_edge(source, i, K, 0)
        g.add_edge(N + i, sink, K, 0)

    A = list(list(map(int, input().split())) for _ in range(N))
    for i, ai in enumerate(A):
        for j, a in enumerate(ai):
            g.add_edge(i, N + j, 1, inf - a)

    res_flow, res_cost = g.flow(source, sink, N * K)
    print(N * K * inf - res_cost)

    res = [["."] * N for _ in range(N)]
    for frm, to, cap, flow, cost in g.edges():
        if frm == source or to == sink or (not flow):
            continue
        res[frm][to - N] = "X"

    for g in res:
        print("".join(g))

Measured memory_bytes: 10304