Skip to main content
1 of 15
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES6), 236 bytes

Expects a matrix of characters.

m=>(w=m[0].length+2,m=[[],...m.map(r=>[,...r]),[]],g=(x,y,z,q=(z&=3)&2,b=z&1||2,r=m[y],v)=>r&&~x&&x<w&&(v=r[x]=+(v=r[x])||(v>g)+(v>{}))^(r[x]|=4<<z)&&(g(x+--q*z%2,y+q*~z%2,z^2),v&b||g(x,y,z-1),v&3-b||g(x,y,z+1),--n))(n=0,0)/4+w*m.length

Try it online!

Arnauld
  • 197.7k
  • 20
  • 179
  • 650