
# AGC014C
def main():
    import sys
    input = lambda : sys.stdin.readline().rstrip()
    sys.setrecursionlimit(max(1000, 10**9))
    write = lambda x: sys.stdout.write(x+"\n")


    h,w,k = map(int, input().split())
    A = [None] * h
    for i in range(h):
        A[i] = input()
        if  "S" in A[i]:
            start = (i, A[i].index("S"))

    dx = [0,1,0,-1]
    dy = [1,0,-1,0]
    hp = [(0, start)] # (距離, ノード番号)
    import heapq
    from collections import defaultdict
    seen = set()
    near = set()
    while hp:
        val, u = heapq.heappop(hp)
        if val<=k:
            near.add(u)
            if u[0]==0 or u[0]==h-1 or u[1]==0 or u[1]==w-1:
                print(1)
                return
        else:
            break
        for i, j in zip(dx,dy):
            xx, yy = u[0]+i, u[1]+j
            if xx<0 or xx>=h or yy<0 or yy>=w:
                continue
            if A[xx][yy]=="#":
                continue
            if not (xx,yy) in seen:
                seen.add((xx,yy))
                heapq.heappush(hp, (val+1, (xx,yy)))
    
    hp = list((0, u) for u in near)
    seen = set()
    dist = {}
    while hp:
        val, u = heapq.heappop(hp)
        for i, j in zip(dx,dy):
            xx, yy = u[0]+i, u[1]+j
            if xx<0 or xx>=h or yy<0 or yy>=w:
                continue
            if not (xx,yy) in seen:
                seen.add((xx,yy))
                heapq.heappush(hp, (val+1, (xx,yy)))
                if xx==0 or xx==h-1 or yy==0 or yy==w-1:
                    dist[xx,yy] = val+1

    def sub():
        best = 10**10
        for i in range(h):
            for j in (0, w-1):
                v = dist[i,j]//k + int(dist[i,j]%k)
                best = min(best, v)
        for j in range(w):
            for i in (0, h-1):
                v = dist[i,j]//k + int(dist[i,j]%k)
                best = min(best, v)
        return best

    print(sub()+1)
main()