No heartworm in dogs is not contagious to either dogs or humans.
So a dog cannot get heartworms from being around another dog who does have heartworms and a human cannot get heartworms from being around the dog who has heartworms.
Heartworm infects a dog through a mosquito bite so if a dog does not get bitten by a mosquito they will not get heartworm.
You should only give your dog heartworm medicine if the dog actually has heartworms as giving the dog heartworm medicine as a preventative measure may actually cause the dog more harm than it does good.
Heartworm medicine can sometimes cause heart issues with a dog and shorten their already short enough lifespan.