(r,c)=gets.split().map(&:to_i)
result=[]
sum=nil
while line=gets
  result<<line.split().map(&:to_i)
  s=0
  result.last.each {|x|s+=x}
  result.last<<s
  if sum
    result.last.each_with_index {|x,i| sum[i]+=x}
  else
    sum=result[0].clone
  end
end
result.each {|x| puts x.join(" ")}
puts sum.join(" ")