def solve():

    from collections import deque

    h, w, k = list(map(int, input().split()))

    maze = [eval(input()) for _ in range(h)]

    visited = [[-1]*w for _ in range(h)]

    for i in range(h):

        if 'S' in maze[i]:

            sy, sx = i, maze[i].index('S')

            break

    queue = deque([[sy, sx]])

    visited[sy][sx] = 0

    r = 10**18

    while queue:

        y, x = queue.popleft()

        r = min(r, y, h-1-y, x, w-1-x)

        if visited[y][x] == k: continue

        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):

            new_y, new_x = y+i, x+j

            if new_y < 0 or new_y >= h or new_x < 0 or new_x >= w: continue

            if maze[new_y][new_x] == '.' and visited[new_y][new_x] == -1:

                visited[new_y][new_x] = visited[y][x] + 1

                queue.append([new_y, new_x])

    print((1 -(-r // k)))

if __name__ == '__main__':

    solve()

from collections import deque;h,w,k=list(map(int,input().split()));m=[eval(input())for _ in[0]*h];v=[[-1]*w for _ in[0]*h];r=9**20

for i in range(h):

 if'S'in m[i]:j=m[i].index('S');q=deque([[i,j]]);v[i][j]=0

while q:

 y,x=q.popleft()

 r=min(r,y,x,h-1-y,w-1-x)

 if v[y][x]==k:continue

 for i,j in([1,0],[-1,0],[0,1],[0,-1]):

  t,u=y+i,x+j

  if t>=0and t<h and u>=0and u<w and m[t][u]=='.'and v[t][u]<0:v[t][u]=v[y][x]+1;q.append([t,u])

print((1-(-r//k)))
def solve():
    from collections import deque

    h, w, k = list(map(int, input().split()))
    maze = [eval(input()) for _ in range(h)]
    visited = [[-1] * w for _ in range(h)]
    for i in range(h):
        if "S" in maze[i]:
            sy, sx = i, maze[i].index("S")
            break
    queue = deque([[sy, sx]])
    visited[sy][sx] = 0
    r = 10**18
    while queue:
        y, x = queue.popleft()
        r = min(r, y, h - 1 - y, x, w - 1 - x)
        if visited[y][x] == k:
            continue
        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
            new_y, new_x = y + i, x + j
            if new_y < 0 or new_y >= h or new_x < 0 or new_x >= w:
                continue
            if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1:
                visited[new_y][new_x] = visited[y][x] + 1
                queue.append([new_y, new_x])
    print((1 - (-r // k)))


if __name__ == "__main__":
    solve()

from collections import deque

h, w, k = list(map(int, input().split()))
m = [eval(input()) for _ in [0] * h]
v = [[-1] * w for _ in [0] * h]
r = 9**20
for i in range(h):
    if "S" in m[i]:
        j = m[i].index("S")
        q = deque([[i, j]])
        v[i][j] = 0
while q:
    y, x = q.popleft()
    r = min(r, y, x, h - 1 - y, w - 1 - x)
    if v[y][x] == k:
        continue
    for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]):
        t, u = y + i, x + j
        if t >= 0 and t < h and u >= 0 and u < w and m[t][u] == "." and v[t][u] < 0:
            v[t][u] = v[y][x] + 1
            q.append([t, u])
print((1 - (-r // k)))

-def solve(): -    from collections import deque +from collections import deque -    h, w, k = list(map(int, input().split())) -    maze = [eval(input()) for _ in range(h)] -    visited = [[-1] * w for _ in range(h)] -    for i in range(h): -        if "S" in maze[i]: -            sy, sx = i, maze[i].index("S") -            break -    queue = deque([[sy, sx]]) -    visited[sy][sx] = 0 -    r = 10**18 -    while queue: -        y, x = queue.popleft() -        r = min(r, y, h - 1 - y, x, w - 1 - x) -        if visited[y][x] == k: -            continue -        for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]): -            new_y, new_x = y + i, x + j -            if new_y < 0 or new_y >= h or new_x < 0 or new_x >= w: -                continue -            if maze[new_y][new_x] == "." and visited[new_y][new_x] == -1: -                visited[new_y][new_x] = visited[y][x] + 1 -                queue.append([new_y, new_x]) -    print((1 - (-r // k))) - - -if __name__ == "__main__": -    solve() +h, w, k = list(map(int, input().split())) +m = [eval(input()) for _ in [0] * h] +v = [[-1] * w for _ in [0] * h] +r = 9**20 +for i in range(h): +    if "S" in m[i]: +        j = m[i].index("S") +        q = deque([[i, j]]) +        v[i][j] = 0 +while q: +    y, x = q.popleft() +    r = min(r, y, x, h - 1 - y, w - 1 - x) +    if v[y][x] == k: +        continue +    for i, j in ([1, 0], [-1, 0], [0, 1], [0, -1]): +        t, u = y + i, x + j +        if t >= 0 and t < h and u >= 0 and u < w and m[t][u] == "." and v[t][u] < 0: +            v[t][u] = v[y][x] + 1 +            q.append([t, u]) +print((1 - (-r // k)))