#JavaScript (ES6), 228 ... 186 184 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>{})+!++v)^(r[x]|=4<<z%4)?g(x+--q*~z%2,y-q*z%2,z^2)&g(x,y,z^=v&1||3)|!(r[x]=v):1:0)(X,Y,0)),n=0)|n