r, c = gets.split.map(&:to_i)
table = []
result_table = []

r.times do
  row = gets.split.map(&:to_i)
  table.push(row)
  result_table.push(row.push(row.sum))
end

column_sumed_row = []
table.transpose.each do |t|
  column_sumed_row.push(t.sum)
end
result_table.push(column_sumed_row)
puts result_table.map{|r|r*' '}
