r,c = gets.split.map {|s| s.to_i}
mat = Array.new(r+1).map {Array.new(c+1,0)}
for i in 1..r
  a = gets.split.map {|s| s.to_i}
  for j in 1..c
    mat[i-1][j-1] = a[j-1]
    mat[i-1][c] += a[j-1]
    mat[r][j-1] += a[j-1]
  end
end
for i in 0..r
  for j in 0..c
    print " " unless j == 0
    print mat[i][j]
  end
  print "\n"
end