
def array_sum(array1)
  length = array1.length
  array_sum = 0
  for i in 0..(length-1)
    array_sum += array1[i]
  end
  return array_sum
end

def column_sum(array2, index)
  length = array2.length
  column_sum = 0
  for i in 0..(length-1)
    column_sum += array2[i][index]
  end
  return column_sum
end

def row_sum(array2)
  length = array2.length
  row_sums = []
  for i in 0..(length-1)
    row_sums.push(array_sum(array2[i]))
  end
  return row_sums
end

def columns_sum(array2, max_column)
  length = array2.length
  columns_sum = []
    for i in 0..(max_column-1)
      columns_sum.push(column_sum(array2, i))
    end
    return columns_sum
end


input_values_rc = gets.chomp.split.map(&:to_i)
r = input_values_rc[0]
c = input_values_rc[1]
input_values_array = []
r.times do
  input_values = gets.chomp.split.map(&:to_i)
  input_values_array.push(input_values)
end #input_values_array???????¬???????????????¢??§?¨????

row_sums = row_sum(input_values_array)
for i in 0..(r-1)
  print input_values_array[i].join(' ')
  print " "
  print row_sums[i]
  print"\n"
end
print columns_sum(input_values_array, c).join(' ')
print " "
print array_sum(columns_sum(input_values_array, c))
print "\n"