JavaScript (ES6), 176171 bytes
Expects a matrix of characters.
f=(m,X,Y=o=f,e=1,n=0)=>m.map((r,y)=>r.map((c,x)=>!~(j="RDLUj="RULD #".search(c))|(n+=j==4,X+1?j>4|(h=x-X)*h+(v=y-Y)*v^1:j<5)?0:j<4?e=h^--j%2|v^~e=j^(v&2)-j%2~h:r[f(m,r[x]=x,y),x]=c))|e|n>o?o:o=n Try it online!Try it online! (only the fastest test cases)