If I understood what you are asking, for every non-red, border tile, flood red. At last the non-red tiles are surrounded by red. In the example starting from top-left corner going anti-clockwise... [![enter image description here][1]][1] [1]: https://i.sstatic.net/DO5px.png