object Main {
  def main(args: Array[String]): Unit = {
    val lines = io.Source.stdin.getLines()
    def loop(it: Iterator[String]): Stream[Table] = {
      readTable(it) match {
        case None => Stream.empty
        case Some((table, it2)) => table #:: loop(it2)
      }
    }
    loop(lines).foreach(printTable)
  }

  case class Table(rows: Seq[Seq[Int]])

  def printTable(table: Table): Unit = {
    val totalRow =
      for (i <- 0 until table.rows.head.length) yield {
        table.rows.map(_.apply(i)).sum
      }
    (table.rows :+ totalRow).foreach{ row =>
      (row :+ row.sum).foreach{ c => print(f"${c}%5d") }
      println()
    }
  }

  def readTable(lines: Iterator[String]): Option[(Table, Iterator[String])] = {
    val n = lines.next().toInt
    if (n <= 0)
      None
    else {
      val (it1, it2) = lines.duplicate
      val table = Table(it1.take(n).map(_.split(" ").map(_.toInt).toSeq).toSeq)
      val rest = it2.drop(n)
      Some((table, rest))
    }
  }
}

