r,c = gets.chomp.split(" ").map{|x| x.to_i}
t = []
t[r] = [0]*(c+1)
for i in 1..r do
  t[i-1] = gets.chomp.split(" ").map{|x| x.to_i}
  t[i-1][c] = t[i-1].inject(:+)
  for j in 0..c do
    t[r][j] += t[i-1][j]
  end
  printf("%s\n",t[i-1].join(" "))
end
printf("%s\n",t[r].join(" "))
