
# AGC014C
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()
end = False
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)
            end = True
            break
    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)))
if not end:
    hp = list((0, u) for u in near)
    dist = {}
    for u in near:
        dist[u] = 0
    while hp:
        val, u = heapq.heappop(hp)
        if u[0]==0 or u[0]==h-1 or u[1]==0 or u[1]==w-1:
            print((val)//k + int((val)%k) + 1)
            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 not (xx,yy) in dist or dist[xx,yy]>val+1:
                heapq.heappush(hp, (val+1, (xx,yy)))
                dist[xx,yy] = val+1
