import sys

class Maze:
    def __init__ (self, H, W, K):
        self.info = {}
        self.info['H'] = H
        self.info['W'] = W
        self.info['K'] = K
        self.maze = []
    def makeMaze(self, Input):
        for i in xrange(self.info['H']):
            line = Input.readline().strip()
            self.maze.append(line)
            Taka = line.find('S')
            if Taka != -1:
                self.Taka = (i, Taka)
    def astar_in_k(self):
        def f(pos):
            k, h = table[pos[0]][pos[1]]
            return h-k
        def valid(pos):
            ret = True                                                                                                                                                                                                                
            ret &= (pos[0] >=0) and (pos[1] >= 0) and (pos[0] < H) and (pos[1]
            < W)
            ret &= not (table.get(pos[0], None) and table[pos[0]].get(pos[1],
            None) )
            if ret:
                ret &= self.maze[pos[0]][pos[1]] == '.'
            return ret
        def update(pos, value):
            table[pos[0]] = table.get(pos[0],{})
            table[pos[0]][pos[1]] = table[pos[0]].get(pos[1],value)
        table = {}
        table[self.Taka[0]] =\
        {self.Taka[1]:(self.info['K'],self.heu(self.Taka) )}
            ## self.heu will return heuristic value to the walls
        route = [self.Taka]
        best = self.Taka
        bestf = self.heu(self.Taka)
        while (route):
            r = route.pop(0)
            k, _ = table[r[0]][r[1]]
            if k == 0:
                break
            # scan for four directions d0~d1
            d = [
            (r[0]-1, r[1]),
            (r[0], r[1]+1),
            (r[0]+1, r[1]),
            (r[0], r[1]-1),
            ]
            for dd in d:
                if valid(dd):
                    update(dd, (k-1, self.heu(dd)))
                    if f(dd) < bestf:
                        best = dd
                        bestf = self.heu(dd)
                        if bestf == 0 or bestf == f(self.Taka) - K:
                            return best, bestf
                            
                        #if bestf == 0 or bestf == f(self.Taka) - K:
                        #    return best, bestf
                    route.append(dd)
            route = sorted(route, key = lambda x: f(x))
        return best, bestf
    def heu(self, pos):
        return min(pos[0], self.info['H']-pos[0], pos[1],
        self.info['W']-pos[1])

    
H,W,K = map(int, sys.stdin.readline().strip().split())
maze = Maze(H,W,K)
maze.makeMaze(sys.stdin)
_, heu = maze.astar_in_k()   
# for first round choose the path that will minimize the manhatan distance of
# Takahashi
print 1+ heu/K + bool(heu%K)            