Let us consider a set $S$ consisting of a collection of grid locations $(i,j),i,j=1,\ldots,n$,  in 2-dimensional gridded plane. Here the point $(i,j)$ corresponds to a rectangular grid in the X-Y plane.

This set has the property that one can reach any element $(i,j)\in S$ from any other element $({i'},{j'})\in S$  through a series of neighbouring grid points which are also in $S$.  

I am interested about the shape of the set $S$ in the plane, for this reason I need the boundary grid locations of the set $S$.

How can I get the boundary grid locations for the set?

I hope this give clear picture about the problem which I am interested in.

Any suggestion and reference will be greatly appreciated.