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

  for i in 1..r
     a = gets.split.map {|s| s.to_i}
    for j in 1..c
      ary[i-1][j-1] = a[j-1]
      ary[i-1][c] += a[j-1]
      ary[r][j-1] += a[j-1]
      ary[r][c] += a[j-1]
    end
  end

  for i in 0..r
    for j in 0..c
      print " " unless j == 0
      print ary[i][j]
    end
    print "\n"
  end
end
