#read matrix size
r, c = gets.split(" ").map(&:to_i)

#prepare array
mat = Array.new(r)
rvec = Array.new(r){0}
cvec = Array.new(c){0}

#read data and calculation part
sum = 0
for i in 0..r-1 do
  mat[i] = gets.chomp
  arr = mat[i].split(" ").map(&:to_i)
  for j in 0..c-1 do
    cvec[j] += arr[j]
    rvec[i] += arr[j]
    sum += arr[j]
  end
  i += 1
end

#output
for i in 0..r-1
  printf "%s %d\n", mat[i], rvec[i]
end
for j in 0..c-1
  printf "%d ", cvec[j]
end
printf "%d\n", sum