Skip to main content
6 of 15
saved 7 bytes
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES6),  228 224 193  186 bytes

Expects a matrix of characters as input.

m=>m.map((r,Y)=>r.map((_,X)=>n+=(g=(x,y,z,q=z&2,r=m[y],v=r&&r[x])=>v?(v=r[x]=+v||64+(v>g)+(v>{}))^(r[x]|=4<<z%4)?g(x+--q*z%2,y+q*~z%2,z^2)&g(x,y,z^=v&1?3:1)|!(r[x]=v):1:0)(X,Y,0)),n=0)|n

Try it online!

Arnauld
  • 197.7k
  • 20
  • 179
  • 650