import qualified Data.Set as S

main = do
 [h,w,k] <- map read . words <$> getLine
 m <- lines <$> getContents
 let ps = S.fromList [(i,j) | i<-[1..h], j<-[1..w], elem (m!!(i-1)!!(j-1)) ".S"]
 let s = head [(i,j) | i<-[1..h], j<-[1..w], m!!(i-1)!!(j-1) == 'S']
 print (escape h w k ps s)

type Height = Int
type Width = Int
type Opened = S.Set Point
type Point = (Int,Int)
type Init = Point

escape :: Height -> Width -> Int -> Opened -> Init -> Int
escape h w k ps s = minimum (map (nmagic h w k) (reachable k ps s))

nmagic :: Height -> Width -> Int -> Point -> Int
nmagic h w k (i,j) = minimum (map (flip div1 k) [i-1,h-i,j-1,w-j]) + 1
 where div1 x y = let (d,m) = divMod x y in d + signum m

reachable :: Int -> Opened -> Point -> [Point]
reachable k ps s = S.toList (apply k (move ps) (S.singleton s))
 where apply n f x = foldr ($) x (replicate n f)

move :: Opened -> S.Set Point -> S.Set Point
move ps rs = unionMap (neighbor ps) rs
 where unionMap f = S.fromList . concatMap f . S.toList

neighbor :: Opened -> Point -> [Point]
neighbor ps (i,j) = filter (flip S.member ps) [(i,j),(i+1,j),(i-1,j),(i,j+1),(i,j-1)]