There are expanding foam products specifically for water and moisture environments…presumably they either exclude the water or convert it….. Do some research and you will find these foams. Buy the right one, but they are not cheap to buy.
An amusing story about these foams,..at least it's amusing 'now'! About 20 years back I replaced a bedroom window and bought some foam which I found being sold in a local boot-sale. All went well until I applied the foam after carefully reading the instructions. I applied the foam from the outside but it kept 'growing' outwards around the window and I started to cut it off and drop the pieces into the front garden below. This expansion continued for quite a long time and when I looked down I was shocked to see that I now had a half-garden of foam lumps! Then a thought finally occured to me,…what might be happening on the inside? I slid down the ladder and raced up the stairs,….. a sense of panic and horror filled my chest when I surveyed the enormous toadstools that were well on th way to filling the bedroom! That foam certainly worked!…when things were back under control I had no less than 9 stuffed black sacks, all from just one can of foam!!
I binnded the other three cans and resolved to always do the research be3fore using any unknown products in future!