//#include "bits/stdc++.h"

#define _USE_MATH_DEFINES
#include<cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <algorithm>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <unordered_map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <iterator>
#include<iomanip>
#include<complex>

using namespace std;

#define rep(i,a,b) for(int i=(a), i##_len=(b);i<i##_len;i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(c) begin(c),end(c)

#define int ll
#define SZ(x) ((int)(x).size())
#define pb push_back
#define mp make_pair

//typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, double> pid;
typedef pair<double, int> pdi;
typedef pair<double, double> pdd;
typedef vector< vector<int> > mat;

template<class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return true; } return false; }

const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;

const double EPS = 1e-9;

struct edge
{
	int to, cap, cost, rev;
	edge(int to, int cap, int cost, int rev) :to(to), cap(cap), cost(cost), rev(rev) {}
	edge() :to(-1), cap(-1), cost(-1), rev(-1) {}
};

vector<vector<edge>> G;
vector<int> h, dist, prevv, preve;

int min_cost_flow(int s, int t, int f)
{
	int res = 0;
	fill(all(h), 0);
	while (f > 0)
	{
		priority_queue<pii, vector<pii>, greater<pii>> q;
		fill(all(dist), INF);
		dist[s] = 0;
		q.push(pii(0, s));
		while (!q.empty())
		{
			pii p = q.top(); q.pop();
			int v = p.second;
			if (dist[v] < p.first)continue;
			rep(i, 0, SZ(G[v]))
			{
				edge& e = G[v][i];
				if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to])
				{
					dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
					prevv[e.to] = v;
					preve[e.to] = i;
					q.push(pii(dist[e.to], e.to));
				}
			}
		}
		if (dist[t] == INF)
		{
			return -1;
		}
		rep(v, 0, SZ(h))h[v] += dist[v];

		int d = f;
		for (int v = t; v != s; v = prevv[v])
		{
			chmin(d, G[prevv[v]][preve[v]].cap);
		}
		f -= d;
		res += d * h[t];
		for (int v = t; v != s; v = prevv[v])
		{
			edge& e = G[prevv[v]][preve[v]];
			e.cap -= d;
			G[v][e.rev].cap += d;
		}
	}
	return res;
}

void add_edge(int from, int to, int cap, int cost)
{
	G[from].push_back(edge(to, cap, cost, SZ(G[to])));
	G[to].push_back(edge(from, 0, -cost, SZ(G[from]) - 1));
}

signed main()
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N, K;
	cin >> N >> K;

	G = vector<vector<edge>>(2 * N + 2);
	h = vector<int>(2 * N + 2, 0);
	dist = vector<int>(2 * N + 2, 0); 
	prevv = vector<int>(2 * N + 2, 0);
	preve = vector<int>(2 * N + 2, 0);

	int NAX = (int)1e10;

	int A;
	rep(i, 0, N)rep(j, 0, N)
	{
		cin >> A;
		add_edge(i, N + j, 1, NAX - A);
	}

	rep(i, 0, N)
	{
		add_edge(2 * N, i, K, 0);
		add_edge(N + i, 2 * N + 1, K, 0);
	}

	add_edge(2 * N, 2 * N + 1, INF, NAX);

	int res = N * K*NAX - min_cost_flow(2 * N, 2 * N + 1, N*K);
	cout << res << endl;
	rep(i, 0, N)
	{
		rep(j, 0, N)
		{
			bool f = false;
			for (auto e : G[i])
			{
				if (e.to == N + j && e.cap == 0)
				{
					f = true;
				}
			}
			if (f)
			{
				cout << 'X';
			}
			else
			{
				cout << '.';
			}
		}
		cout << endl;
	}

	return 0;
}

Measured memory_bytes: 3832