// http://yukicoder.me/problems/37 open System open System.Collections.Generic let dpfn fmt = Printf.kprintf Diagnostics.Debug.WriteLine fmt let dir = [1,0; 0,-1; -1,0; 0,1] [] let main argv = let W, H = Console.ReadLine().Trim().Split([|' '|]) |> Array.map int |> fun a -> a.[0], a.[1] let M = Array.init H (fun _ -> Console.ReadLine().Trim().Split([|' '|]) |> Array.map int) let memo = Array2D.init W H (fun _ _ -> false) let rec check_loop c hist queue = if Set.count queue = 0 then false else let depth, x, y = Set.maxElement queue let queue' = Set.remove (depth, x, y) queue memo.[x,y] <- true if Map.tryFind (x,y) hist <> None then true else let hist' = Map.add (x,y) depth hist let queue' = dir |> List.map (fun (dx,dy) -> x+dx,y+dy) |> List.filter (fun (x,y) -> x>=0 && y>=0 && x List.filter (fun (x,y) -> M.[y].[x] = c && (memo.[x,y] = false || Map.tryFind (x,y) hist |> function | Some d -> depth+1-d>=4 | None -> false)) |> List.fold (fun q (x,y) -> Set.add (depth+1,x,y) q) queue' check_loop c hist' queue' let rec loop x y = if y = H then false else if x = W then loop 0 (y+1) else if memo.[x,y] then loop (x+1) y else let ans = check_loop M.[y].[x] Map.empty (Set.singleton (0,x,y)) if ans then ans else loop (x+1) y if loop 0 0 then "possible" else "impossible" |> printfn "%s" 0 // 整数の終了コードを返します