import sys

sys.setrecursionlimit(10**7)

dx = (1, 0, -1, 0,)
dy = (0, 1, 0, -1,)

inf = 1<<60

def solve():
    def dfs(i, j, a, visited):
        visited[i][j] = True

        for l in range(len(dx)):
            ni = i + dy[l]
            nj = j + dx[l]

            if 0 <= ni < h and 0 <= nj < w:
                if (not visited[ni][nj]) and a[ni][nj]:
                    dfs(ni, nj, a, visited)

    h, w, k = map(int, sys.stdin.readline().split())

    si = -1
    sj = -1

    a = []

    for i in range(h):
        s = sys.stdin.readline().rstrip()
        a.append([ch != '#' for ch in s])

        if 'S' in s:
            si = i
            sj = s.find('S')

    # print(a)
    # print(si, sj)

    visited = [[False]*w for i in range(h)]

    dfs(si, sj, a, visited)

    # print(visited)

    ans = inf

    for i in range(h):
        for j in range(w):
            if visited[i][j]:
                tmp = min([h - 1 - i, i, w - 1 - j, j])
                tmp = (tmp + k - 1) // k + 1
                ans = min(ans, tmp)

    print(ans)

if __name__ == '__main__':
    solve()