Added C=zeros(n+2,m+2); to speed up the algorithm