
r,c=gets.split(" ").map(&:to_i)
h = Array.new(r+1).map{Array.new(c+1,0)} 

for i in 1..r

a= gets.split.map(&:to_i)
    for j in 1..c
    h[i-1][j-1] = a[j-1]
    h[i-1][c] += a[j-1]
    h[r][j-1] += a[j-1]
    h[r][c] += a[j-1]
    end
end
for i in 1...r
    for j in 1...c
      print " " unless j == 1
      print h[i][j]
    end
    print "\n"
  end

