#JavaScript (ES6), <s>&nbsp;228 224 193&nbsp;</s> 186 bytes

Expects a matrix of characters as input.

<!-- language-all: lang-javascript -->

    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!](https://tio.run/##TZDBcoIwEIbveYr0IGYlglqnB8fgqX0BL3aMdhgESgeCBMoIYl@dbuDQHrL/v5vv38zky6/9MtDJtZqr/BL2kegz4WVO5l8Z0/wdhKfH5oMfsFG2YLFgN97wlheitVZci@zYnHgttGXp4@2EVL1j2KIXdt11L2ub1V4Mpt4fAGdmrjqx3m7byRp2MbvZ83kxaycr3tjF7MeY9rwCKx4fOovaWu6eN0vonoasqGGz3CyAHfg7XwBwJRbQqV6HxXeiQzaNyik4OvQvb0ka7hsVsAU4Vb6vdKJiBk55TZOKTaWSCsEo169@8MlKKjx6J5QGuSrzNHTSPGYRK/9whM1naAMeHcfRJwAgD@hd6UpKJKVUEoreJcR1XSklCs5QKAISCYmOSCRdIxIpRDHxrxDDEpMjw6phaBJmNcVragBK0RjImEHHEB2DBsVDhjnOyEAaxlRcSX8B "JavaScript (Node.js) – Try It Online")