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
    
    ans = min(sx,H-sx-1,sy,W-sy-1)
    q = deque([(sx,sy)])
    c = 0
    while (q and c!=K):
        c += 1
        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-1 and 0<ny<W-1):
                if (room[nx][ny] == "." and dist[nx][ny] == -1):
                    dist[nx][ny] = 1
                    ans = min(ans,nx,H-nx-1,ny,W-ny-1)
                    q.append((nx,ny))
            else:
                if room[nx][ny] == ".":
                    print(1)
                    exit()
                
    print(2+(ans-1)//K)            

if __name__ == "__main__":
    main()