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
  y = 0
  for h in 0..r-1
    y += arr[h][k]
  end
  arr[r][k] = y
end

for l in 0..r
    for x in 0..c
        print "#{arr[l][x]} "
    end
    puts "\n"
end