you have 4 nested 4 loops.
From a quick glance, it doesn't look like the innermost code has any obvious shortcuts or way to speed things up (much).

If you want even more speed, then the trick now...