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
    if k == 0:
        V[pi][pj] = False
        return pos
    opt = pos
    if (not V[pi - 1][pj]) and pi - 1 >= 1 and A[pi - 1][pj] != '#':
        V[pi - 1][pj] = True
        pos2 = __opt_start((pi - 1, pj), vec, k - 1)
        opt = __opt_pos(opt, pos2, vec)

    if (not V[pi + 1][pj]) and pi + 1 <= H and A[pi + 1][pj] != '#':
        V[pi + 1][pj] = True
        pos2 = __opt_start((pi + 1, pj), vec, k - 1)
        opt = __opt_pos(opt, pos2, vec)

    if (not V[pi][pj - 1]) and pj - 1 >= 1 and A[pi][pj - 1] != '#':
        V[pi][pj - 1] = True
        pos2 = __opt_start((pi, pj - 1), vec, k - 1)
        opt = __opt_pos(opt, pos2, vec)

    if (not V[pi][pj + 1]) and pj + 1 <= W and A[pi][pj + 1] != '#':
        V[pi][pj + 1] = True
        pos2 = __opt_start((pi, pj + 1), vec, k - 1)
        opt = __opt_pos(opt, pos2, vec)
    V[pi][pj] = False
    return opt


# def __A(pos):
#     return A[pos[0]][pos[1]]


def __opt_pos(pos1, pos2, vec):
    vi, vj = vec
    i1, j1 = pos1
    i2, j2 = pos2
    if vec == (0, 1):
        return pos1 if j1 > j2 else pos2
    elif vec == (0, -1):
        ret = pos1 if j1 < j2 else pos2
        # print(vec, pos1, pos2, ret)
        return ret
    elif vec == (1, 0):
        return pos1 if i1 > i2 else pos2
    elif vec == (-1, 0):
        return pos1 if i1 < i2 else pos2
    else:
        return None


def __mahou(sta, vec, B):
    mahou = 0
    pi, pj = sta
    A = B[:]
    goal = False
    while not goal:
        mahou += 1
        vi, vj = vec

        if mahou == 1:
            pi, pj = __opt_start(sta, vec, K)
            # print(vec, pi, pj)
            goal = pi == 1 or pi == H or pj == 1 or pj == W
        else:
            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

        if goal:
            break

        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
    return mahou
#
# sta1 = __opt_start(S, S, (-1, 0), K, A)
# sta2 = __opt_start(S, S, (+1, 0), K, A)
# sta3 = __opt_start(S, S, (0, -1), K, A)
# sta4 = __opt_start(S, S, (0, +1), K, A)


ans = 10000000
ans = min(ans, __mahou(S, (-1, 0), A))
ans = min(ans, __mahou(S, (1, 0), A))
ans = min(ans, __mahou(S, (0, -1), A))
ans = min(ans, __mahou(S, (0, 1), A))
print(ans)

# 5 7 2
# ######
# ######
# ##S###
# ##..##
# ######
