Sure thing. What I do is to give each subdivision a different colour to the ones around it (a la the four colour theorem but I usually use the entire PDN palette), and use your method to first off erase the borders in the source image, one colour at a time (so do your method on the red tile to highlight the bordering black pixels and fill them in, then blue, then green, then yellow, then back to red for example) until you’re left with only the colours you used for the subdivisions. Then you implement your method again on the subdivision colours IN THE REVERSE ORDER to the one you used to erase the borders beforehand in order to make them one pixel wide and the correct colour.
I generally start from the top down and do first national borders, then first level, second level, etc. Each uses the same process.