Extract boundary of 2D grids