Magnetic nanostructures stabilized by surface-state electrons