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

for i in 0..r-1
 for j in 0..c-1
arr[i][j] = gets.split.chomp.map(&:to_i)
end

r.times do
csum = 0
for i in 0..r
 for j in 0..c-1
csum += arr[i][j]
end
arr[i][c] = csum
end
end

c.times do
rsum = 0
 for i in 0..c-1
  for j in 0..r-1
 rsum += arr[j][i]
end 
arr[r][i] = rsum
end
end


puts arr.join("\n")

