from collections import deque
H, W, K = map(int, input().split())
A = [list(input()) for _ in range(H)]
for i in range(H):
    for j in range(W):
        if A[i][j] == 'S':
            sx = i
            sy = j
            break
dist = [[-1]*W for _ in range(H)]
dist[sx][sy] = 0
que = deque([(sx, sy)])
dire = [(1, 0), (-1, 0), (0, 1), (0, -1)]
m = min(sx, sy, H-1-sx, W-1-sy)
while que:
    x, y = que.pop()
    if m==0:
        break
    d = dist[x][y]
    if d == K:
        break
    for dx, dy in dire:
        nx = x+dx
        ny = y+dy
        if dist[nx][ny]<0 and A[nx][ny] == '.':
            dist[nx][ny] = d+1
            que.append((nx, ny))
            tmp = min(nx, ny, H-1-nx, W-1-ny)
            m = min(tmp, m)
print(m//K+1)            
        
    
    