import Data.List import Text.Printf g::Double->Double->String->[(Double,Double)] g _ _ ""=[] g h w ('0':xs)= g h (w+1) xs g h w (_:xs) =(h,w):(g h (w+1) xs) f::Double->[String]->[(Double,Double)] f _ []=[] f h (x:xs)=(g h 1 x) ++ (f (h+1) xs) f2::Double->Double->(Double,Double)->Double f2 h w (y,x)=sqrt((y-h)*(y-h)+(x-w)*(x-w)) main = do e<-getLine es<-getContents let ts=lines es ds=f 1 ts (h:w:[])=map read (words e)::[Int] anss=[sum(map (f2 (fromIntegral y::Double) (fromIntegral(x)::Double)) ds)|y<-[0..(h+1)],x<-[0..(w+1)],y==0||y==h+1||x==0||x==w+1] printf "%.8f" $ minimum anss