$0 = 1 $1 = 1 while($1<=H && $0 <= W) if(A[$1,$0]==B[$1,$0]) return A[$1,$0] end $0=$0+$ if($0==W+1) $0=0; $1=$1+1 end end