import qualified Data.Set as S
import Data.Char

main = do
 [h,w,k] <- map read . words <$> getLine
 m <- filter isPrint <$> getContents
 let ix = [(i,j) | i<-[1..h], j<-[1..w]]
 let ps = map fst (filter (flip elem ".S" . snd) (zip ix m))
 let s = fst (head (filter ((=='S') . snd) (zip ix m)))
 print (escape h w k ps s)

type Height = Int
type Width = Int
type Opened = [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 = apply k (move ps) [s]
 where apply n f x = foldr ($) x (replicate n f)

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

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