import qualified Data.IntSet as S
import Data.Char
 
div1 x y = let (d,m) = divMod x y in d + signum m
 
unionMap f s = S.foldr (\e acc -> S.union acc (f e)) S.empty s
 
main = do
 [h,w,k] <- map read . words <$> getLine
 m <- filter isPrint <$> getContents
 let ixm = zip [(i,j) | i<-[1..h], j<-[1..w]] m
 let (si,sj) = fst (head (filter ((=='S') . snd) ixm))
 let cand ((i,j),c) = elem c ".S" && abs (si-i) + abs (sj-j) <= k
 let ps = S.fromDistinctAscList (map ((\(i,j) -> i*1000+j) . fst) (filter cand ixm))
 print (escape h w k ps (1000*si + sj))
 
type Height = Int
type Width = Int
type Opened = S.IntSet
type Point = 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 ij = minimum (map (flip div1 k) [i-1,h-i,j-1,w-j]) + 1
 where (i,j) = divMod ij 1000
 
reachable :: Int -> Opened -> Point -> Opened
reachable k ps s = reachable' k (S.singleton s) (S.singleton s)
 where
  reachable' 0 acc _ = acc
  reachable' k' acc b = reachable' (k'-1) (S.union acc next) (S.difference next acc)
   where next = unionMap (neighbor ps) b

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