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

r.times {
  arr.push(gets.split.map(&:to_i))
}

r.times { |i|
  arr[i][c] = arr[i].inject{ |sum, j| sum + j }
}

for k in 0..c-1
  y = 0
  for h in 0..r-1
    y += arr[h][k]
  end
  arr[r][k] = y
end

arr[r][c] = arr[r].inject{ |sum, t| sum + t }

(0..r).each{ |r|
    puts arr[r].join(" ")
}