r,c=gets.split(' ')
r=r.to_i
c=c.to_i
a=Array.new(r){Array.new(c)}

for i in 0..r-1 do
    total=0
    a[i]=gets.split(' ').map(&:to_i)
    for j in 0..c-1 do
        total+=a[i][j]
    end
    a[i].push(total)
end

b=Array.new()
for j in 0..c do
    total=0
    for i in 0..r-1 do
        total+=a[i][j]
    end
    b.push(total)
end
a.push(b)

a.each{|e|
  e.each_with_index{|x,i|
    if i<e.length-1 then
    printf("%d ",x)
    else
    printf("%d\n",x)
    end
  }
}