# frozen_string_literal: true

# Priority Queue
# Reference: https://github.com/python/cpython/blob/master/Lib/heapq.py
class PriorityQueue
  # By default, the priority queue returns the maximum element first.
  # If a block is given, the priority between the elements is determined with it.
  # For example, the following block is given, the priority queue returns the minimum element first.
  # `PriorityQueue.new { |x, y| x < y }`
  #
  # A heap is an array for which a[k] <= a[2*k+1] and a[k] <= a[2*k+2] for all k, counting elements from 0.
  def initialize(array = [], &comp)
    @heap = array
    @comp = comp || proc { |x, y| x > y }
    heapify
  end

  attr_reader :heap

  # Push new element to the heap.
  def push(item)
    shift_down(0, @heap.push(item).size - 1)
  end

  alias << push
  alias append push

  # Pop the element with the highest priority.
  def pop
    latest = @heap.pop
    return latest if empty?

    ret_item = heap[0]
    heap[0] = latest
    shift_up(0)
    ret_item
  end

  # Get the element with the highest priority.
  def get
    @heap[0]
  end

  alias top get

  # Returns true if the heap is empty.
  def empty?
    @heap.empty?
  end

  private

  def heapify
    (@heap.size / 2 - 1).downto(0) { |i| shift_up(i) }
  end

  def shift_up(pos)
    end_pos = @heap.size
    start_pos = pos
    new_item = @heap[pos]
    left_child_pos = 2 * pos + 1

    while left_child_pos < end_pos
      right_child_pos = left_child_pos + 1
      if right_child_pos < end_pos && @comp.call(@heap[right_child_pos], @heap[left_child_pos])
        left_child_pos = right_child_pos
      end
      # Move the higher priority child up.
      @heap[pos] = @heap[left_child_pos]
      pos = left_child_pos
      left_child_pos = 2 * pos + 1
    end
    @heap[pos] = new_item
    shift_down(start_pos, pos)
  end

  def shift_down(star_pos, pos)
    new_item = @heap[pos]
    while pos > star_pos
      parent_pos = (pos - 1) >> 1
      parent = @heap[parent_pos]
      break if @comp.call(parent, new_item)

      @heap[pos] = parent
      pos = parent_pos
    end
    @heap[pos] = new_item
  end
end

HeapQueue = PriorityQueue

# Min Cost Flow Grapsh
class MinCostFlow
  def initialize(n)
    @n      = n
    @pos    = []
    @g_to   = Array.new(n) { [] }
    @g_rev  = Array.new(n) { [] }
    @g_cap  = Array.new(n) { [] }
    @g_cost = Array.new(n) { [] }
    @pv     = Array.new(n)
    @pe     = Array.new(n)
    @dual   = Array.new(n) { 0 }
  end

  def add_edge(from, to, cap, cost)
    edge_number = @pos.size

    @pos << [from, @g_to[from].size]

    from_id = @g_to[from].size
    to_id   = @g_to[to].size
    to_id += 1 if from == to

    @g_to[from]   << to
    @g_rev[from]  << to_id
    @g_cap[from]  << cap
    @g_cost[from] << cost

    @g_to[to]     << from
    @g_rev[to]    << from_id
    @g_cap[to]    << 0
    @g_cost[to]   << -cost

    edge_number
  end

  def get_edge(i)
    from, id = @pos[i]
    to = @g_to[from][id]
    rid = @g_rev[from][id]
    [from, to, @g_cap[from][id]+@g_cap[to][rid], @g_cap[to][rid], @g_cost[from][id]]
  end
  alias edge get_edge
  alias [] get_edge

  def edges
    @pos.map do |(from, id)|
      to = @g_to[from][id]
      rid = @g_rev[from][id]
      [from, to, @g_cap[from][id]+@g_cap[to][rid], @g_cap[to][rid], @g_cost[from][id]]
    end
  end

  def flow(s, t, flow_limit = Float::MAX)
    slope(s, t, flow_limit).last
  end

  def dual_ref(s, t)
    dist = Array.new(@n, Float::MAX)
    @pv.fill(-1)
    @pe.fill(-1)
    vis = Array.new(@n, false)

    que = PriorityQueue.new { |par, chi| par[0] < chi[0] }
    dist[s] = 0
    que.push([0, s])

    while (v = que.pop)
      v = v[1]

      next if vis[v]

      vis[v] = true
      break if v == t

      @g_to[v].size.times do |i|
        to = @g_to[v][i]
        next if vis[to] || @g_cap[v][i] == 0

        cost = @g_cost[v][i] - @dual[to] + @dual[v]
        next unless dist[to] - dist[v] > cost

        dist[to] = dist[v] + cost
        @pv[to] = v
        @pe[to] = i
        que.push([dist[to], to])
      end
    end

    return false unless vis[t]

    @n.times do |i|
      next unless vis[i]

      @dual[i] -= dist[t] - dist[i]
    end

    true
  end

  def slope(s, t, flow_limit = Float::MAX)
    flow = 0
    cost = 0
    prev_cost = -1
    result = [[flow, cost]]

    while flow < flow_limit
      break unless dual_ref(s, t)

      c = flow_limit - flow
      v = t
      while v != s
        c = @g_cap[@pv[v]][@pe[v]] if c > @g_cap[@pv[v]][@pe[v]]
        v = @pv[v]
      end

      v = t
      while v != s
        nv = @pv[v]
        id = @pe[v]
        @g_cap[nv][id] -= c
        @g_cap[v][@g_rev[nv][id]] += c
        v = nv
      end

      d = -@dual[s]
      flow += c
      cost += c * d
      result.pop if prev_cost == d
      result << [flow, cost]
      prev_cost = d
    end

    result
  end
end

MCF      = MinCostFlow
MCFGraph = MinCostFlow

n, k = gets.split.map &:to_i
a = $<.map{ |e| e.split.map &:to_i }

g = MinCostFlow.new(2*n+2)
s, t = 2*n, 2*n+1
inf = 10**9

n.times{ |i|
  g.add_edge(s, i, k, 0)
  g.add_edge(i, t, k, inf)
  g.add_edge(n+i, t, k, 0)
}
n.times{ |i|
  n.times{ |j|
    g.add_edge(i, n+j, 1, inf-a[i][j])
  }
}

puts inf*n*k - g.flow(s, t)[1]

ans = (0 ... n).map{ '.' * n }
g.edges.each{ |from, to, cap, flow, _|
  next if from==s || to==t
  ans[from][to-n] = cap==flow ? 'X' : '.'
}

ans.each{ |s| puts s }


Measured memory_bytes: 15144