import Data.Bool import Data.List main = do [w,h] <- map read . words <$> getLine ms <- map read . words <$> getContents :: IO [Int] putStrLn (bool "impossible" "possible" (anyClosed w h ms)) anyClosed w h ms = or (unfoldr (isClosed w) ims) where ims = zip [i | i<-[1..(w+1)*h], mod i (w+1) /= 0] ms isClosed w [] = Nothing isClosed w ((i,m):ims) = Just (dfs w [i,i] is, ims) where is = map fst (filter ((==m) . snd) ((i,m):ims)) dfs w acc@(a:a':as) is | null is = False | notElem a is = False | elem a as = True | otherwise = or (map (\i -> dfs w (i:acc) is) (delete a' [a-1,a+1,a-(w+1),a+(w+1)]))