Finding shortest distance.
Precomputing images.