x = gets()
y = x.split()
r = y[0].to_i
c = y[1].to_i
fir = []
msum = 0
i = 0
while i < r
 xx = gets()
 yy = xx.split()
 yyy = yy.map{|a| a.to_i}
 h = 0
 sum = 0
 while h < c
  sum += yyy[h]
  if fir[h].nil?
   fir[h] = yyy[h]
  else
   fir[h] += yyy[h]
  end
  h += 1
 end
 yyy << sum
 msum += sum
 puts yyy.join(" ")
 i += 1
end

fin = fir << msum
puts fin.join(" ")
