r, c = gets.split.map{|v|
      v.to_i
}
ary = Array.new(r+1){ Array.new(c+1) }

for i in 1..r
  ary[i-1] = gets.split(" ").map(&:to_i)
end
for i in 1..r
  n = 0
  for j in 1..c
    n += ary[i-1][j-1]
  end
  ary[i-1][j] = n
end

for j in 1..c
  n = 0
  for i in 1..r
    n += ary[i-1][j-1]
  end
  ary[i][j-1] = n
end
n = 0
for i in 1..r
  n += ary[i-1][c]
end
ary[r][c] = n

for i in 1..r+1
  for j in 1..c+1
    print ary[i-1][j-1], " "
  end
  print "\n"
end
