#include "bits/stdc++.h"
using namespace std;
using     ll=long long;
using    vll=vector<   ll>;
using   vvll=vector<  vll>;
using  vvvll=vector< vvll>;
using vvvvll=vector<vvvll>;
using pll=pair<ll,ll>; using tll=tuple<ll,ll,ll>; using qll=tuple<ll,ll,ll,ll>;

constexpr ll INF = 1LL << 60;
struct Fast{ Fast(){ cin.tie(0); ios::sync_with_stdio(false); cout<<fixed<<setprecision(numeric_limits<double>::max_digits10); } } fast;
#define REPS(i, S, E) for (ll i = (S); i <= (E); i++)
#define REP(i, N) REPS(i, 0, (N)-1)
#define DEPS(i, S, E) for (ll i = (E); i >= (S); i--)
#define DEP(i, N) DEPS(i, 0, (N)-1)
#define rep(i, S, E)  for (ll i = (S); i <= (E); i++)
#define dep(i, E, S)  for (ll i = (E); i >= (S); i--)
#define each(e, v) for (auto&& e : v)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; }return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; }return false; }
template<class T> inline T MaxE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmax(m,v[i]); return m; }
template<class T> inline T MinE(vector<T>&v,ll S,ll E){ T m=v[S]; rep(i,S,E)chmin(m,v[i]); return m; }
template<class T> inline T MaxE(vector<T> &v) { return MaxE(v,0,(ll)v.size()-1); }
template<class T> inline T MinE(vector<T> &v) { return MinE(v,0,(ll)v.size()-1); }
template<class T> inline T Sum(vector<T> &v,ll S,ll E){ T s=T(); rep(i,S,E)s+=v[i]; return s; }
template<class T> inline T Sum(vector<T> &v) { return Sum(v,0,v.size()-1); }
template<class T> inline ll sz(T &v){ return (ll)v.size(); }
inline ll CEIL(ll a,ll b){ return (a<0) ? -(-a/b) : (a+b-1)/b; }
inline ll FLOOR(ll a,ll b){ return -CEIL(-a,b); }

#if 1
#include <atcoder/all>
using namespace atcoder;
#endif

struct Mcf_graph{
	mcf_graph<int,ll> mcfg;
	Mcf_graph(){}
	Mcf_graph(ll n): mcfg(int(n)) {}
	ll add_edge(ll from,ll to,ll cap,ll cost) {
		return (ll)mcfg.add_edge(int(from),int(to),int(cap),cost);
	}
	mcf_graph<int,ll>::edge get_edge(ll i){ return mcfg.get_edge(int(i)); }
	vector<mcf_graph<int,ll>::edge> edges() { return mcfg.edges(); }
	pair<ll,ll> flow(ll s,ll t) {
		pair<int,ll> ret=mcfg.flow(int(s),int(t));
		return pair<ll,ll>{ret.first,ret.second};
	}
	pair<ll,ll> flow(ll s,ll t,ll flow_limit) {
		pair<int,ll> ret=mcfg.flow(int(s),int(t),int(flow_limit));
		return pair<ll,ll>{ret.first,ret.second};
	}
	vector<pair<int,ll>> slope(ll s,ll t) { return mcfg.slope(int(s),int(t)); }
	vector<pair<int,ll>> slope(ll s,ll t,ll flow_limit) {
		return mcfg.slope(int(s),int(t),int(flow_limit));
	}
};

void solve()
{
#if 1
	const ll BIG=1000000000;
	ll n,k;  cin >> n >> k;
	vector<vector<ll>> a(n,vector<ll>(n));
	rep(i,0,n-1)rep(j,0,n-1){ ll a_; cin>>a_; a[i][j]=a_; }

	Mcf_graph graph(2*n+2);
	vvll egid(n,vll(n));

	ll s=2*n,t=2*n+1;
	rep(v,0,n-1){
		graph.add_edge(s,v,k,0);
	}
	rep(v,n,2*n-1){
		graph.add_edge(v,t,k,0);
	}
	rep(v,0,n-1)rep(u,0,n-1){
		egid[v][u]=graph.add_edge(v,u+n,1,BIG-a[v][u]);
	}
	graph.add_edge(s,t,n*k,BIG);

	auto fc=graph.flow(s,t,k*n);

	cout << n*k*BIG-fc.second << '\n';
	vector<string> ans(n,string(n,'.'));
	rep(v,0,n-1)rep(u,0,n-1){
		auto eg=graph.get_edge(egid[v][u]);
		if (eg.flow==1) ans[v][u]='X';
	}
	each(e,ans) cout << e << '\n';
#endif
}


int main(){
#if 1
	solve();
#else
	ll t;  cin >> t;
	rep(i, 0, t-1){
		solve();
	}
#endif
	return 0;
}


Measured memory_bytes: 3996