Time: O(n*n*l), Space: O(n*n)