r, c = gets.split(" ").map(&:to_i)
a = []

r.times { |t|
  a[t] = gets.chomp.split.map(&:to_i)
}

r.times { |i|
  a[i][c] = a[i].inject{ |sum, j| sum + j }
}

for k in 0..c
  y = 0
  for h in 0..r-1
    y += a[h][k]
  end
  a[r][k] = y
end

(0..r).each{ |l|
    puts a[l].join(" ")
}