import os
import sys
from heapq import heappop, heappush

import numpy as np


def solve(n, k, aaa):
    MINCOSTFLOW_LINKS = []
    INF = 10 ** 10

    def mincostflow_init(n):
        lst = [[0]]
        lst.clear()
        MINCOSTFLOW_LINKS.append([lst.copy() for _ in range(n)])
        return len(MINCOSTFLOW_LINKS) - 1

    def mincostflow_add_link(ins, frm, to, capacity, cost):
        links = MINCOSTFLOW_LINKS[ins]
        links[frm].append([to, capacity, cost, len(links[to])])
        links[to].append([frm, 0, -cost, len(links[frm]) - 1])

    def mincostflow_flow(ins, s, t, f):
        links = MINCOSTFLOW_LINKS[ins]
        n = len(links)
        res = 0
        hierarchy = np.zeros(n, dtype=np.int64)
        dist = np.full(n, INF, dtype=np.int64)
        prev_v = np.full(n, -1, dtype=np.int64)
        prev_e = np.full(n, -1, dtype=np.int64)

        while f:
            dist.fill(INF)
            dist[s] = 0
            que = [(0, s)]

            while que:
                total_cost, v = heappop(que)
                # print(total_cost, v)
                if dist[v] < total_cost:
                    continue
                dv = dist[v] + hierarchy[v]
                for i, (u, cap, cost, _) in enumerate(links[v]):
                    new_cost = dv + cost - hierarchy[u]
                    if cap > 0 and new_cost < dist[u]:
                        dist[u] = new_cost
                        prev_v[u] = v
                        prev_e[u] = i
                        heappush(que, (new_cost, u))

            # Cannot flow quantity f
            if dist[t] == INF:
                return -1

            hierarchy += dist

            cur_flow = f
            v = t
            while v != s:
                cur_flow = min(cur_flow, links[prev_v[v]][prev_e[v]][1])
                v = prev_v[v]
            f -= cur_flow
            res += cur_flow * hierarchy[t]

            v = t
            while v != s:
                link = links[prev_v[v]][prev_e[v]]
                link[1] -= cur_flow
                links[v][link[3]][1] += cur_flow
                v = prev_v[v]

        return res

    ins = mincostflow_init(n * 2 + 2)
    s = n + n
    t = s + 1

    mincostflow_add_link(ins, s, t, n * k, INF)
    for i in range(n):
        mincostflow_add_link(ins, s, i, k, 0)
        mincostflow_add_link(ins, i + n, t, k, 0)
        for j in range(n):
            mincostflow_add_link(ins, i, j + n, 1, INF - aaa[i][j])
    ans = INF * (n * k) - mincostflow_flow(ins, s, t, n * k)

    matrix = np.zeros((n, n), dtype=np.int8)
    links = MINCOSTFLOW_LINKS[ins]
    for i in range(n):
        for link in links[i]:
            if link[0] == t or link[1] == 1 or link[2] <= 0:
                continue
            j = link[0] - n
            matrix[i][j] = 1

    return ans, matrix


SIGNATURE = '(i8,i8,i8[:,:],)'
if sys.argv[-1] == 'ONLINE_JUDGE':
    from numba.pycc import CC

    cc = CC('my_module')
    cc.export('solve', SIGNATURE)(solve)
    cc.compile()
    exit()

if os.name == 'posix':
    # noinspection PyUnresolvedReferences
    from my_module import solve
else:
    from numba import njit

    solve = njit(SIGNATURE, cache=True)(solve)
    print('compiled', file=sys.stderr)

n, k = map(int, sys.stdin.readline().split())
aaa = np.fromstring(sys.stdin.read(), dtype=np.int64, sep=' ').reshape(n, n)
ans, matrix = solve(n, k, aaa)

print(ans)
CHARS = '.X'.__getitem__
for row in matrix:
    print(''.join(map(CHARS, row)))


Measured memory_bytes: 28064