#include <bits/stdc++.h>

#ifdef DEBUG
#include <Mylib/Debug/debug.cpp>
#else
#define dump(...) ((void)0)
#endif

template <typename T, typename U>
bool chmin(T &a, const U &b){
  return (a > b ? a = b, true : false);
}

template <typename T, typename U>
bool chmax(T &a, const U &b){
  return (a < b ? a = b, true : false);
}

template <typename T, size_t N, typename U>
void fill_array(T (&a)[N], const U &v){
  std::fill((U*)a, (U*)(a + N), v);
}

template <typename T, size_t N, size_t I = N>
auto make_vector(const std::array<int, N> &a, T value = T()){
  static_assert(I >= 1);
  static_assert(N >= 1);
  if constexpr (I == 1){
    return std::vector<T>(a[N - I], value);
  }else{
    return std::vector(a[N - I], make_vector<T, N, I - 1>(a, value));
  }
}

template <typename T>
std::ostream& operator<<(std::ostream &s, const std::vector<T> &a){
  for(auto it = a.begin(); it != a.end(); ++it){
    if(it != a.begin()) s << " ";
    s << *it;
  }
  return s;
}

template <typename T>
std::istream& operator>>(std::istream &s, std::vector<T> &a){
  for(auto &x : a) s >> x;
  return s;
}

/**
 * @title Minimum cost flow
 * @docs minimum_cost_flow.md
 */
namespace haar_lib {
  template <typename T, typename U>
  class minimum_cost_flow {
  public:
    struct edge {
      int from, to;
      T cap;
      U cost;
      int rev;
      bool is_rev;
      edge(int from, int to, T cap, U cost, int rev, bool is_rev):
        from(from), to(to), cap(cap), cost(cost), rev(rev), is_rev(is_rev){}
    };

  private:
    int size;
    std::vector<std::vector<edge>> g;

  public:
    minimum_cost_flow(){}
    minimum_cost_flow(int size): size(size), g(size){}

    void add_edge(int from, int to, T cap, U cost){
      g[from].emplace_back(from, to, cap, cost, g[to].size(), false);
      g[to].emplace_back(to, from, 0, -cost, g[from].size() - 1, true);
    }

    std::pair<T, U> solve(int src, int dst, const T &f){
      using P = std::pair<U, int>;
      U ret = 0;
      T flow = f;
      std::vector<U> h(size, 0), cost(size);
      std::vector<bool> is_inf(size, true);
      std::vector<int> prev_node(size), prev_edge(size);
      std::priority_queue<P, std::vector<P>, std::greater<P>> pq;

      while(flow > 0){
        std::fill(is_inf.begin(), is_inf.end(), true);

        // src -> dst の最小コスト経路を探索する。 (dijkstra algorithm)
        cost[src] = 0;
        pq.emplace(0, src);
        is_inf[src] = false;

        while(!pq.empty()){
          U c;
          int v;
          std::tie(c, v) = pq.top(); pq.pop();

          if(cost[v] < c) continue;
          for(int i = 0; i < (int)g[v].size(); ++i){
            edge &e = g[v][i];
            int w = e.to;
            T cap = e.cap;
            U cst = e.cost;
            if(cap > 0){
              if(is_inf[w] or cost[w] + h[w] > cost[v] + h[v] + cst){
                is_inf[w] = false;
                cost[w] = cost[v] + cst + h[v] - h[w];
                prev_node[w] = v;
                prev_edge[w] = i;
                pq.emplace(cost[w], w);
              }
            }
          }
        }

        if(is_inf[dst]) return {f - flow, ret}; // dstへ到達不可能

        for(int i = 0; i < size; ++i) h[i] += cost[i];

        // src -> dst の最小コスト経路へ流せる量(df)を決定する。
        T df = flow;
        for(int cur = dst; cur != src; cur = prev_node[cur]){
          df = std::min(df, g[prev_node[cur]][prev_edge[cur]].cap);
        }

        flow -= df;
        ret += df * h[dst];

        // capの更新
        for(int cur = dst; cur != src; cur = prev_node[cur]){
          edge &e = g[prev_node[cur]][prev_edge[cur]];
          e.cap -= df;
          g[cur][e.rev].cap += df;
        }
      }

      return {f - flow, ret};
    }

    std::vector<edge> edges() const {
      std::vector<edge> ret;
      for(auto &v : g) ret.insert(ret.end(), v.begin(), v.end());
      return ret;
    }
  };
}



namespace haar_lib {}

namespace solver {
  using namespace haar_lib;

  void init(){
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(12);
    std::cerr << std::fixed << std::setprecision(12);
    std::cin.exceptions(std::ios_base::failbit);
  }

  void solve(){
    int N, K; std::cin >> N >> K;

    auto A = make_vector<int64_t, 2>({N, N});
    std::cin >> A;

    minimum_cost_flow<int64_t, int64_t> m(N * 2 + 2);

    int s = N * 2, t = s + 1;

    m.add_edge(s, t, INT_MAX, 0);

    for(int i = 0; i < N; ++i){
      m.add_edge(s, i, K, 0);
      m.add_edge(i + N, t, K, 0);
    }


    for(int i = 0; i < N; ++i){
      for(int j = 0; j < N; ++j){
        m.add_edge(i, j + N, 1, -A[i][j]);
      }
    }

    std::cout << -m.solve(s, t, K * N).second << "\n";

    std::vector<std::string> S(N, std::string(N, '.'));
    auto edges = m.edges();
    for(auto &e : edges){
      if(e.from != s and e.to != t and not e.is_rev and e.cap == 0){
        S[e.from][e.to - N] = 'X';
      }
    }

    for(auto &a : S) std::cout << a << "\n";
  }
}

int main(){
  solver::init();
  while(true){
    try{
      solver::solve();
    }catch(const std::istream::failure &e){
      break;
    }
  }
  return 0;
}


Measured memory_bytes: 4328