import qualified Data.Set as S
import qualified Data.ByteString.Char8 as B
import Data.Char

apply n f x = foldr ($) x (replicate n f)

div1 x y = let (d,m) = divMod x y in d + signum m

unionMap f s = foldr add s s
 where add e acc = let s' = f e in if S.isSubsetOf s' s then acc else S.union acc s'

main = do
 [h,w,k] <- map read . words <$> getLine
 m <- B.filter isPrint <$> B.getContents
 let ixm = zip [(i,j) | i<-[1..h], j<-[1..w]] (B.unpack m)
 let (sx,sy) = fst (head (filter ((=='S') . snd) ixm))
 let cand ((i,j),c) = elem c ".S" && abs (sx-i) + abs (sy-j) <= k
 let ps = S.fromDistinctAscList (map fst (filter cand ixm))
 print (escape h w k ps (sx,sy))

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) (S.elems (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

reachable :: Int -> Opened -> Point -> Opened
reachable k ps s = apply k (move ps) (S.singleton s)

move :: Opened -> Opened -> Opened
move ps rs = unionMap (neighbor ps) rs

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