line = STDIN.gets.split(" ")
size = Array.new(2) { |i| i = line[i].to_i }

matrix = []

for i in 0..size[0] - 1 do
  line = STDIN.gets.split(" ")
  for j in 0..size[1] - 1 do
    # puts "i = #{i}, j = #{j}"
    line[j] = line[j].to_i
  end
  matrix << line
  # print matrix
end

for i in 0..size[0] - 1 do
  sum = 0
  matrix[i].each do |x|
    sum += x
  end
  matrix[i][size[1]] = sum
end

# print matrix, "\n"

sum_line = []
for j in 0..size[1] do
  sum = 0
  for i in 0..size[0] - 1 do
    # puts "i = #{i}, j = #{j}"
    sum += matrix[i][j]
    # puts "sum = #{sum}"
  end
  sum_line << sum
end
matrix << sum_line

# print matrix , "\n"

matrix.each do |x|
  x.each do |y|
    print y
    if x.length != y - 1
      print " "
    end
  end
  print "\n"
end