Networking together hundreds or thousands of cheap sensor nodes allows users to accurately monitor a remote environment by intelligently combining the data from the individual nodes. As sensor nodes are typically battery operated, it is important to efficiently use the limited energy of the nodes to extend the lifetime of the wireless sensor network (WSN). One of the fundamental issues in WSNs is the coverage problem. In this paper, the border coverage problem in WSNs is rigorously analyzed. Most existing results related to the coverage problem in wireless sensor networks focused on planar networks; however, three dimensional (3D) modeling of the sensor network would reflect more accurately real-life situations. Unlike previous works in this area, we provide distributed algorithms that allow the selection and activation of an optimal border cover for both 2D and 3D regions of interest. We also provide self-healing algorithms as an optimization to our border coverage algorithms which allow the sensor network to adaptively reconfigure and repair itself in order to improve its own performance. Border coverage is crucial for optimizing sensor placement for intrusion detection and a number of other practical applications.