import numpy as np
H, W, K = map(int, input().split())
ar = []
for _ in range(H):
    ar.append([c for c in input()])
ar = np.array(ar)
sh, sw = np.where(ar == 'S')
sh=sh[0]
sw=sw[0]
flag = 1
count = 0
ard = np.array([H*W]*W*H).reshape(H,W)
ar[sh,sw] = 0
ard[sh, sw] = min(sh, H-1 - sh, sw, W-1 - sw) 
check = np.array([[sh, sw]])
ans = 1
while flag and count < K:
    flag = 0
    count += 1
    s = np.array([[1,0],[0,1],[-1,0],[0,-1]])
    add_check = []
    for d in s:
        cos = check + d
        np.delete(check, 0)
        for co in cos:
            if co[0] >= 0 and co[0] <= H-1 and co[1] >= 0 and co[1] <= W-1:
                if ar[co[0],co[1]] == '.':
##                    if co[0]==0 or co[0]==H-1 or co[1]==0 or co[1]==W-1:
##                        ans = 0
                    ar[co[0],co[1]] = count
    ##                ar[co[0],co[1]] = min(co[0] - 0, H-1 - co[0], co[1] - 0, W-1 - co[1])
                    ard[co[0],co[1]] = min(co[0] - 0, H-1 - co[0], co[1] - 0, W-1 - co[1])
                    flag = 1

                    add_check.append(co)

                    np.append(check, [co],axis = 0)

    check = np.array(add_check)

dist = np.min(ard)

print(ans + (dist-1) // K + 1)

