# -*- coding: utf-8 -*-

import sys
import os
import math

H, W, K = map(int, input().split())
#print(H, W, K)

map = []
for s in sys.stdin:
    s = s.strip()
    map.append(list(s))
#print(map)

# search S
for i, row in enumerate(map):
    if 'S' in row:
        x = row.index('S')
        y = i
        break

# 初期値から行ける場所全てをPに列挙する
P = []
Q = []
Q.append((x, y, 0))
visited = [[False] * 800 for i in range(800)]
dist = [[False] * 800 for i in range(800)]

def is_accessible(x, y):
    if 0 <= x <= W-1 and 0 <= y <= H-1 and not visited[y][x] and map[y][x] == '.':
        return True
    else:
        return False

while Q:
    px, py, d = Q.pop(0)
    P.append((px, py, d))
    visited[py][px] = True
    dist[py][px] = d

    if d < K:
        # 上下左右
        if is_accessible(px+1, py):
            Q.append((px+1, py, d+1))
        if is_accessible(px-1, py):
            Q.append((px-1, py, d+1))
        if is_accessible(px, py+1):
            Q.append((px, py+1, d+1))
        if is_accessible(px, py-1):
            Q.append((px, py-1, d+1))

#print(P)
min_dist = 10000
min_dist_p = None
for p in P:
    # その点から上下左右に向かったときの最薄の壁の厚さ
    x = p[0]
    y = p[1]
    dist_to_wall = min(x, y, W - x - 1, H - y - 1)
    #print('p', p, dist_to_wall)
    if dist_to_wall < min_dist:
        min_dist = dist_to_wall
        min_dist_p = p

x = min_dist_p[0]
y = min_dist_p[1]
#print('min p', min_dist_p, 'dist', min_dist, 'そこまでの距離', dist[y][x])

# 1発目でそこに行き、あとは魔法して進むのみ
#print('壁の厚さ', min_dist)
magic_num = math.ceil(min_dist / K)
print(int(magic_num) + 1)