h,w,k = map(int, raw_input().split())
a = []
visited = {}
ans = []
for _ in range(h):
  row = list(raw_input())
  a.append(row)


s = 0
for i in range(h):
  for j in range(w):
    if a[i][j] == 'S':
      s = (i,j)

# def visit(i,j,ki):
q = [(s[0], s[1], k)]
while len(q) > 0:
  i,j,ki = q.pop(0)

  if ki < 0:
    continue
  if i < 0 or h <= i:
    continue
  if j < 0 or w <= j:
    continue
  if a[i][j] == '#':
    continue
  if (i,j) in visited:
    continue

  visited[(i,j)] = True
  d = min(h-1-i, i, w-1-j, j)
  dk = (d+k-1) / k
  ans.append(dk)

  q.append((i+1, j, ki-1))
  q.append((i-1, j, ki-1))
  q.append((i, j+1, ki-1))
  q.append((i, j-1, ki-1))

# visit(s[0], s[1], k)
# print visited
print min(ans)+1