import Data.Bool import Data.List import qualified Data.IntSet as S 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 = any (isClosed w ims) (group (sort ms)) where ims = zip [i | i<-[1..(w+1)*h], mod i (w+1) /= 0] ms isClosed w ims (n:ns) = sum (degree w is) >= length (n:ns) * 2 where is = map fst (filter ((==n).snd) ims) degree w is = map (\i -> S.size (S.intersection (S.fromList [i-1,i+1,i-(w+1),i+(w+1)]) is')) is where is' = S.fromDistinctAscList is