The next time they will flash again will be n seconds after 9:00:00 pm, where n is the least common multiple (LCM) of 56 and 40. To find the LCM, we look at the prime factorizations of the two values and take the least product of primes which is divisible by both.
56 = 2^3xx7
40 = 2^3 xx 5
We can see that the LCM requires at least three factors of 2, one factor of 5, and one factor of 7. Thus
LCM(40, 56) = 2^3xx5xx7 = 280.
As 280 seconds is equal to 4 minutes and 40 seconds, we can add that value to 9:00:00 pm to find that the next time they flash at the same time is at 9:04:40pm.
Note that at that time, the first lighthouse will have flashed 280/56 = 5 times, and the second will have flashed 280/40 = 7 times.