H, W, K = map(int, input().split())
O = '_'
A = [O + input() + O for _ in range(H)]
A.insert(H, O * (W + 2))
A.insert(0, O * (W + 2))

# for a in A:
    # print(a)

sta = None
for i in range(1, H + 1):
    for j in range(1, W + 1):
        if (A[i][j] == 'S'):
            sta = (i, j)
            break


def __mahou(sta, vec, B):
    mahou = 0
    pi, pj = sta
    A = B[:]

    while not (pi == 1 or pi == H or pj == 1 or pj == W):
        mahou += 1
        vi, vj = vec
        k = K
        while k > 0 and A[pi + vi][pj + vj] != '#':
            pi, pj = pi + vi, pj + vj
            # if A[pi][pj] == O:
            if pi == 1 or pi == H or pj == 1 or pj == W:
                break
            k -= 1
        k = K
        ri, rj = pi + vi, pj + vj
        while k > 0 and ri < len(A) and rj < len(A[ri]) and A[ri][rj] != O:
            if A[ri][rj] == '#':
                A[ri] = A[ri][:rj] + '.' + A[ri][rj + 1:]
                k -= 1
            ri, rj = ri + vi, rj + vj
    return mahou

ans = 1000000
ans = min(ans, __mahou(sta, (-1, 0), A))
ans = min(ans, __mahou(sta, (1, 0), A))
ans = min(ans, __mahou(sta, (0, -1), A))
ans = min(ans, __mahou(sta, (0, 1), A))
print(ans)
