Another version using Warshall-Floyd algorithm