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):
q = [(s[0], s[1], k)]
while len(q) > 0:
  i,j,ki = q.pop(0)

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

  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

  if 0 <= j < w:
    if 0 <= i+1 < h and a[i+1][j] != '#' and (i+1,j) not in visited:
      q.append((i+1, j, ki-1))
    if 0 <= i-1 < h and a[i-1][j] != '#' and (i-1,j) not in visited:
      q.append((i-1, j, ki-1))

  if 0 <= i < h:
    if 0 <= j+1 < w and a[i][j+1] != '#' and (i,j+1) not in visited:
      q.append((i, j+1, ki-1))
    if 0 <= j-1 < w and a[i][j-1] != '#' and (i,j-1) not in visited:
      q.append((i, j-1, ki-1))

  # 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 ans+1