wall = "#"
space = "."

h, w, k = map(int, input().split())

ats = []
def search(ypos, xpos, depth):
    que = [[ypos, xpos, depth]]
    while que:
        ypos, xpos, depth = que.pop()
        if depth < k:
            if (0 <= ypos < h - 1) and (0 <= xpos < w - 1) and cfield[ypos][xpos] == "." or cfield[ypos][xpos] == "S":
                cfield[ypos][xpos] = "N"
                que.append([ypos + 1, xpos, depth + 1])
                que.append([ypos - 1, xpos, depth + 1])
                que.append([ypos, xpos + 1, depth + 1])
                que.append([ypos, xpos - 1, depth + 1])
                at = min(
                        abs(0 - ypos),
                        abs(0 - xpos),
                        abs(h - 1 - ypos),
                        abs(w - 1 - xpos),
                        )
                ats.append(at)
    # print(1)
    # __import__("sys").exit()


field = []
cfield = []
for i in range(h):
    line = input()
    if "S" in line:
        spos = [i, line.index("S")]
    field.append(line)
    cfield.append(list(line))

search(spos[0], spos[1], 0)
# print(spos)
# for i in cfield:
#     print("".join(i))
#
# print(ats)
print((min(ats) + (k - 1)) // k + 1)
