If I have understood your query correctly you have a custom entity, lets say A which has many to many relationship with both account and contact. And you want to have say a sub-grid of custom entity A on contact form where Custom Entity A is associated with
Contact's parent account.
This can not be handled OOB as per best of my knowledge. You may have to write a code to achieve that.
http://missdynamicscrm.blogspot.in/2014/08/crm-2013-using-addcustomfilter-to-get-filtered-lookup-field-based-on-linked-entity.html
Hope it helps!
Regards, Abhishek Bakshi If you find this post helpful then please Vote as Helpful and Mark As Answer. Check my blog on https://mydynamicscrmblog.wordpress.com/