Added the case elseif (C(B(jjj,1)+dx,B(jjj,2)+dy)==1)