figured out m+n nearest index iteration