H, W, K = map(int, input().split())
PAD = '_'
A = [PAD + input() + PAD for _ in range(H)]
A = [PAD * (W + 2)] + A + [PAD * (W + 2)]
V = [[False for _ in range(W + 2)] for _ in range(H + 2)]

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


def __opt_start(pos, vec, k):
    pi, pj = pos
    score = 0
    V[pi][pj] = True
    if k == 0:
        score = __score(pos, vec)
        return pos, vec, score

    opt, optvec, optscore = pos, vec, score
    if (not V[pi - 1][pj]) and pi - 1 >= 1 and A[pi - 1][pj] != '#':
        p, v, s = __opt_start((pi - 1, pj), (-1, 0), k - 1)
        if s >= optscore:
            opt, optvec, optscore = p, v, s

    if (not V[pi + 1][pj]) and pi + 1 <= H and A[pi + 1][pj] != '#':
        p, v, s = __opt_start((pi + 1, pj), (+1, 0), k - 1)
        if s >= optscore:
            opt, optvec, optscore = p, v, s

    if (not V[pi][pj - 1]) and pj - 1 >= 1 and A[pi][pj - 1] != '#':
        p, v, s = __opt_start((pi, pj - 1), (0, -1), k - 1)
        if s >= optscore:
            opt, optvec, optscore = p, v, s

    if (not V[pi][pj + 1]) and pj + 1 <= W and A[pi][pj + 1] != '#':
        p, v, s = __opt_start((pi, pj + 1), (0, +1), k - 1)
        if s >= optscore:
            opt, optvec, optscore = p, v, s

    return opt, optvec, optscore


def __score(pos, vec):
    vi, vj = vec
    i, j = pos
    if vec == (0, 1):
        return W - j
    elif vec == (0, -1):
        return j - 1
    elif vec == (1, 0):
        return H - i
    elif vec == (-1, 0):
        return i - 1
    else:
        return None


def __mahou(sta, vec):
    mahou = 1
    pi, pj = sta
    goal = pi == 1 or pi == H or pj == 1 or pj == W
    while not goal:
        vi, vj = vec
        k = K
        ri, rj = pi + vi, pj + vj
        while k > 0 and not(ri == 0 or ri == H + 1 or rj == 0 or rj == W + 1):
            if A[ri][rj] == '#':
                A[ri] = A[ri][:rj] + '.' + A[ri][rj + 1:]
                k -= 1
            ri, rj = ri + vi, rj + vj

        mahou += 1
        k = K
        while k > 0 and A[pi + vi][pj + vj] != '#':
            pi, pj = pi + vi, pj + vj
            goal = pi == 1 or pi == H or pj == 1 or pj == W
            if goal:
                break
            k -= 1

    return mahou

sta, vec, score = __opt_start(S, None, K)
if vec is None:
    l, r, u, d = (S[0] - 1, W - S[0], S[1] - 1, H - S[1])
    m = min([l, r, u, d])
    if l == m:
        vec = (-1, 0)
    elif r == m:
        vec = (+1, 0)
    elif u == m:
        vec = (0, -1)
    elif d == m:
        vec = (0, +1)
ans = __mahou(sta, vec)
print(ans)
