Unoptimized solution for 362 (runs in 10mins).