import sys
#input = sys.stdin.buffer.readline
from collections import deque

def main():
    H,W,K = map(int,input().split())
    room = [list(map(str,input())) for _ in range(H)]
    dist = [[-1 for _ in range(W)] for _ in range(H)]
    
    for i in range(H):
        for j in range(W):
            if room[i][j] == "S":
                sx,sy = i,j
                dist[i][j] = 0
    
    if (sx == 0 or sx == H-1 or sy == 0 or sy == W-1):
        print(0)
        exit()
                
    q = deque([(sx,sy)])
    cs = [(sx,sy)]
    while q:
        x,y = q.pop()
        dx,dy = [1,0,-1,0],[0,1,0,-1]
        for i in range(4):
            nx,ny = x+dx[i],y+dy[i]
            if (0<=nx<H and 0<=ny<W and room[nx][ny] == "." and dist[nx][ny] == -1):
                if (nx == 0 or nx == H-1 or ny == 0 or ny == W-1):
                    print(1)
                    exit()
                else:
                    dist[nx][ny] = dist[x][y] + 1
                    cs.append((nx,ny))
                    if dist[nx][ny] != K:
                        q.append((nx,ny))

    ans = 10**3
    for x,y in cs:
        ans = min(ans,-(-x//K),-(-(H-1-x)//K),-(-y//K),-(-(W-1-y)//K))
        
    print(ans+1)

if __name__ == "__main__":
    main()