import math

h, w, k = [int(i) for i in input().split()]

tk = 0

a = [None for j in range(h)]

for i in range(h):
    a[i] = input()

for i in range(h):
    for j in range(w):
        if a[i][j] == 'S':
            si = i
            sj = j

def distance(i,j):
    mind = min([i, j, w-i, w-j])
    # print(i, j, w-i, w-j)
    # print(mind)
    return mind

min_d = distance(si,sj)
# print('check')7 7 2

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

# pi = si
# pj = sj

def tansaku(pi,pj):
    global min_d
    global tk

    if tk > k:
        return

    if visited[pi][pj]==True:
        return

    visited[pi][pj] = True

    min_d = min(min_d, distance(pi,pj))

    if min_d == 0:
        return

    ni = pi
    nj = pj-1
    if a[ni][nj]=='.':
        tk += 1
        tansaku(ni,nj)
        tk -= 1

    ni = pi
    nj = pj+1
    if a[ni][nj]=='.':
        tk += 1
        tansaku(ni,nj)
        tk -= 1

    ni = pi-1
    nj = pj
    if a[ni][nj]=='.':
        tk += 1
        tansaku(ni,nj)
        tk -= 1

    ni = pi+1
    nj = pj
    if a[ni][nj]=='.':
        tk += 1
        tansaku(ni,nj)
        tk -= 1


tansaku(si,sj)
print(math.ceil(min_d/k)+1)

# for i in range(k):
