def read_inputs
  r, c = gets.split(" ").map(&:to_i)
  (1..r).map { gets.split(" ").map(&:to_i)}
end

def sum_row(row)
  row << row.inject(:+)
end

def sum_column(table)
  table.transpose.map {|c| c.inject(:+)}
end

table = read_inputs
puts table.map{|r| sum_row(r).join(" ")}.join("\n")
puts sum_column(table).join(" ")