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

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

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


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


puts arr.join("\n")

