h, w, kk = [int(i) for i in input().split()]
mi = [[10 ** 18] * w for _ in range(h)]

a = []
q = []
for l in range(h):
    s = input()
    b = []
    for j, k in enumerate(s):
        b.append(k != "#")
        if k == "S":
            q.append([l, j, 0])
            mi[l][j] = 0
    a.append(b)

k = 0
while True:
    if k == len(q):
        break
    i, j, c = q[k]
    k += 1
    if c < kk:
        e = j
        for d in range(i - 1, 1 + 2, 2):
            if 0 <= d < h and 0 <= e < w and a[d][e]:
                    if mi[d][e] != kk:
                        mi[d][e] = kk
                        q.append([d, e, c + 1])
                        
                        if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                            print(1)
                            exit()
        d = i
        for e in range(j - 1, j + 2, 2):
            if 0 <= d < h and 0 <= e < w and a[d][e]:
                
                if mi[d][e] !=kk:
                    mi[d][e] = kk
                    q.append([d, e, c + 1])
                    
                    if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                        print(1)
                        exit()
    else:
        break

k = 0
while True:
    i, j, c = q[k]
    k+=1
    e = j
    for d in range(i-1, 1+2, 2):
        if 0 <= d < h and 0 <= e < w:
            if mi[d][e] > c + 1:
                mi[d][e] = c + 1
                q.append([d, e, c + 1])
                
                if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                    print(c//kk+1)
                    exit()
    d = i
    for e in range(j-1, j+2, 2):
        if 0 <= d < h and 0 <= e < w:
            if mi[d][e] > c + 1:
                mi[d][e] = c + 1
                q.append([d, e, c + 1])
                
                if d == 0 or d == h - 1 or e == 0 or e == w - 1:
                    print(c//kk+1)
                    exit()