I=input
N,M=map(int,I().split())
a=x=y=0
D={'U':['D',0,2,-M,M,2,2],'R':['L',2,0,0,2,-M,M],'D':['U',0,-2,-M,M,0,0],'L':['R',-2,0,-2,0,-M,M]}
for i in range(N):
	C=[0]
	for c in I():
		d,e,f,g,h,i,j=D[c]
		x+=e;y+=f
		if g<=x<=h and i<=y<=j:
			if C[-1]==d:C.pop()
			else:C+=[c]
	a+=len(C)<2
print(a)