h,w,k = map(int, raw_input().split())
a = []
visited = {}
ans = float('inf')
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):
  global ans
  if ki == 0:
    return
  if i < 0 or h <= i:
    return
  if j < 0 or w <= j:
    return
  if a[i][j] == '#':
    return
  if (i,j) in visited:
    return

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

  visit(i+1, j, ki-1)
  visit(i-1, j, ki-1)
  visit(i, j+1, ki-1)
  visit(i, j-1, ki-1)

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