Haskell
import Data.Array
import Data.Char
import qualified Data.Map as M
import qualified Data.Set as S
solve1 g = sum $ map (bfs g 1 . S.singleton) xs
where xs = [ i | (i,e) <- assocs g, e == 0 ]
bfs _ 10 xs = S.size xs
bfs g n xs = bfs g (n+1) (S.fromList ys)
where ys = concatMap (steps g n) $ S.elems xs
steps g n (r,c) = filter valid [(r-1,c),(r,c+1),(r+1,c),(r,c-1)]
where valid y = inRange (bounds g) y && g ! y == n
solve2 g = sum $ map (bfs2 g 1 . M.fromList . (:[])) xs
where xs = [ (i,1) | (i,e) <- assocs g, e == 0 ]
bfs2 _ 10 xs = sum $ M.elems xs
bfs2 g n xs = bfs2 g (n+1) (M.fromListWith (+) ys)
where ys = [ (y,a) | (x,a) <- M.assocs xs, y <- steps g n x ]
readGrid xss = listArray ((1,1),(h,w)) $ concatMap (map digitToInt) xss
where h = length xss
w = length $ head xss
main = do g <- readGrid . lines <$> readFile "input"
print $ solve1 g
print $ solve2 g