In many applications, I would add ghost elements at the edges of the array (extra rows and columns of zeros at the edges), and then just loop over the interior. See also Arrays with periodic boundaries - #4 by stevengj
Another advantage of ghost elements is that they are seasonal.