import sys

input = sys.stdin.readline



import itertools



# 初日以外は何も気にせず動けるとしてよい。初日に到達可能な場所のうち、最もゴールに近いものはどこか



H,W,K = list(map(int,input().split()))

A = '#' * (W+2)

for _ in range(H):

    A += '#' + input().rstrip() + '#'

A += '#' * (W+2)



start = A.find('S')

dxs = (1,-1,(W+2),-(W+2))



visited = set()

q = [start]

visited.add(start)

for _ in range(K):

    qq = []

    for x,dx in itertools.product(q,dxs):

        y = x+dx

        if A[y] == '#':

            continue

        if y in visited:

            continue

        visited.add(y)

        qq.append(y)

    q = qq



nearest = 10**9

for x in visited:

    row,col = divmod(x,W+2)

    d = min(row-1,H-row,col-1,W-col)

    if nearest > d:

        nearest = d



answer = 1 + (nearest+K-1)//K

print(answer)
import sys

import numpy as np



read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines



def main(A, H, W, K, S):

    INF = 10**9

    dist = np.full(H * W, INF, np.int32)

    q = np.empty(H * W + 100, np.int32)

    q[:2] = 2, 2



    def push(q, x):

        q[q[1]] = x

        q[1] += 1



    def pop(q):

        q[0] += 1

        return q[q[0] - 1]



    def is_empty(q):

        return q[0] == q[1]



    move = (1, -1, W, -W)

    dist[S] = 0

    push(q, S)

    while not is_empty(q):

        v = pop(q)

        for d in (1, -1, W, -W):

            w = v + d

            if A[w] == '#' or dist[w] <= dist[v] + 1:

                continue

            dist[w] = dist[v] + 1

            push(q, w)

    dist = dist.reshape(H, W)

    min_d = INF

    for h in range(1, H - 1):

        for w in range(1, W - 1):

            if dist[h, w] > K:

                continue

            d = min(h - 1, H - 2 - h, w - 1, W - 2 - w)

            min_d = min(d, min_d)

    return 1 + (min_d + K - 1) // K



if sys.argv[-1] == 'ONLINE_JUDGE':

    import numba

    from numba.pycc import CC

    i8 = numba.from_dtype(np.int64)

    u1 = numba.from_dtype(np.dtype('U1'))

    signature = (u1[:], i8, i8, i8, i8)



    cc = CC('my_module')

    cc.export('main', signature)(main)

    cc.compile()



from my_module import main



H, W, K = list(map(int, readline().split()))

A = np.full((H + 2, W + 2), '#', 'U1')

A[1:-1, 1:-1] = np.frombuffer(read(), 'S1').reshape(H, -1)[:, :W]



H, W = A.shape

A = A.ravel()

S = np.where(A == 'S')[0][0]



print((main(A, H, W, K, S)))
import sys

input = sys.stdin.readline
import itertools

# 初日以外は何も気にせず動けるとしてよい。初日に到達可能な場所のうち、最もゴールに近いものはどこか
H, W, K = list(map(int, input().split()))
A = "#" * (W + 2)
for _ in range(H):
    A += "#" + input().rstrip() + "#"
A += "#" * (W + 2)
start = A.find("S")
dxs = (1, -1, (W + 2), -(W + 2))
visited = set()
q = [start]
visited.add(start)
for _ in range(K):
    qq = []
    for x, dx in itertools.product(q, dxs):
        y = x + dx
        if A[y] == "#":
            continue
        if y in visited:
            continue
        visited.add(y)
        qq.append(y)
    q = qq
nearest = 10**9
for x in visited:
    row, col = divmod(x, W + 2)
    d = min(row - 1, H - row, col - 1, W - col)
    if nearest > d:
        nearest = d
answer = 1 + (nearest + K - 1) // K
print(answer)

import sys
import numpy as np

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


def main(A, H, W, K, S):
    INF = 10**9
    dist = np.full(H * W, INF, np.int32)
    q = np.empty(H * W + 100, np.int32)
    q[:2] = 2, 2

    def push(q, x):
        q[q[1]] = x
        q[1] += 1

    def pop(q):
        q[0] += 1
        return q[q[0] - 1]

    def is_empty(q):
        return q[0] == q[1]

    move = (1, -1, W, -W)
    dist[S] = 0
    push(q, S)
    while not is_empty(q):
        v = pop(q)
        for d in (1, -1, W, -W):
            w = v + d
            if A[w] == "#" or dist[w] <= dist[v] + 1:
                continue
            dist[w] = dist[v] + 1
            push(q, w)
    dist = dist.reshape(H, W)
    min_d = INF
    for h in range(1, H - 1):
        for w in range(1, W - 1):
            if dist[h, w] > K:
                continue
            d = min(h - 1, H - 2 - h, w - 1, W - 2 - w)
            min_d = min(d, min_d)
    return 1 + (min_d + K - 1) // K


if sys.argv[-1] == "ONLINE_JUDGE":
    import numba
    from numba.pycc import CC

    i8 = numba.from_dtype(np.int64)
    u1 = numba.from_dtype(np.dtype("U1"))
    signature = (u1[:], i8, i8, i8, i8)
    cc = CC("my_module")
    cc.export("main", signature)(main)
    cc.compile()
from my_module import main

H, W, K = list(map(int, readline().split()))
A = np.full((H + 2, W + 2), "#", "U1")
A[1:-1, 1:-1] = np.frombuffer(read(), "S1").reshape(H, -1)[:, :W]
H, W = A.shape
A = A.ravel()
S = np.where(A == "S")[0][0]
print((main(A, H, W, K, S)))

+import numpy as np -input = sys.stdin.readline -import itertools +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines -# 初日以外は何も気にせず動けるとしてよい。初日に到達可能な場所のうち、最もゴールに近いものはどこか -H, W, K = list(map(int, input().split())) -A = "#" * (W + 2) -for _ in range(H): -    A += "#" + input().rstrip() + "#" -A += "#" * (W + 2) -start = A.find("S") -dxs = (1, -1, (W + 2), -(W + 2)) -visited = set() -q = [start] -visited.add(start) -for _ in range(K): -    qq = [] -    for x, dx in itertools.product(q, dxs): -        y = x + dx -        if A[y] == "#": -            continue -        if y in visited: -            continue -        visited.add(y) -        qq.append(y) -    q = qq -nearest = 10**9 -for x in visited: -    row, col = divmod(x, W + 2) -    d = min(row - 1, H - row, col - 1, W - col) -    if nearest > d: -        nearest = d -answer = 1 + (nearest + K - 1) // K -print(answer) + +def main(A, H, W, K, S): +    INF = 10**9 +    dist = np.full(H * W, INF, np.int32) +    q = np.empty(H * W + 100, np.int32) +    q[:2] = 2, 2 + +    def push(q, x): +        q[q[1]] = x +        q[1] += 1 + +    def pop(q): +        q[0] += 1 +        return q[q[0] - 1] + +    def is_empty(q): +        return q[0] == q[1] + +    move = (1, -1, W, -W) +    dist[S] = 0 +    push(q, S) +    while not is_empty(q): +        v = pop(q) +        for d in (1, -1, W, -W): +            w = v + d +            if A[w] == "#" or dist[w] <= dist[v] + 1: +                continue +            dist[w] = dist[v] + 1 +            push(q, w) +    dist = dist.reshape(H, W) +    min_d = INF +    for h in range(1, H - 1): +        for w in range(1, W - 1): +            if dist[h, w] > K: +                continue +            d = min(h - 1, H - 2 - h, w - 1, W - 2 - w) +            min_d = min(d, min_d) +    return 1 + (min_d + K - 1) // K + + +if sys.argv[-1] == "ONLINE_JUDGE": +    import numba +    from numba.pycc import CC + +    i8 = numba.from_dtype(np.int64) +    u1 = numba.from_dtype(np.dtype("U1")) +    signature = (u1[:], i8, i8, i8, i8) +    cc = CC("my_module") +    cc.export("main", signature)(main) +    cc.compile() +from my_module import main + +H, W, K = list(map(int, readline().split())) +A = np.full((H + 2, W + 2), "#", "U1") +A[1:-1, 1:-1] = np.frombuffer(read(), "S1").reshape(H, -1)[:, :W] +H, W = A.shape +A = A.ravel() +S = np.where(A == "S")[0][0] +print((main(A, H, W, K, S)))