package main
import (
  "container/heap"
  "fmt"
)
const INF = 1000000000
func main() {
  var h,w,k int
  var s string
  fmt.Scan(&h,&w,&k)
  g := make([][]int,h*w+1)
  opened := make([]bool,h*w)
  x,y := -1,-1
  for i:=0;i<h;i++ {
    fmt.Scan(&s)
    for j:=0;j<w;j++ {
      id := w*i+j
      if s[j:j+1] != "#" { opened[id] = true }
      if s[j:j+1] == "S" { x,y = i,j }
      if i == 0 || i == h-1 || j == 0 || j == w-1 { g[h*w] = append(g[h*w],id) }
      if i != 0 {
        g[id] = append(g[id],w*(i-1)+j)
        g[w*(i-1)+j] = append(g[w*(i-1)+j],id)
      }
      if i != h-1 {
        g[id] = append(g[id],w*(i+1)+j)
        g[w*(i+1)+j] = append(g[w*(i+1)+j],id)
      }
      if j != 0 {
        g[id] = append(g[id],w*i+j-1)
        g[w*i+j-1] = append(g[w*i+j-1],id)
      }
      if j != w-1 {
        g[id] = append(g[id],w*i+j+1)
        g[w*i+j+1] = append(g[w*i+j+1],id)
      }
    }
  }
  c := dijkstra(g,h*w)
  m := INF
  for i:=0;i<h;i++ {
    for j:=0;j<w;j++ {
      id := w*i+j
      if abs(i-x)+abs(j-y) <= k && opened[id] { m = min(m,(c[id]+k-2)/k+1) }
    }
  }
  fmt.Println(m)
}
func abs(n int) int {
  if n < 0 { return -n }
  return n
}
func min(n,m int) int {
  if n < m { return n }
  return m
}
func dijkstra(g [][]int,start int) []int {
  n := len(g)
  c := make([]int,n)
  for i:=0;i<n;i++ { c[i] = INF }
  var pq PriorityQueue
  heap.Init(&pq)
  heap.Push(&pq,&Item{start,0})
  c[start] = 0
  for pq.Len() > 0 {
    s := heap.Pop(&pq).(*Item)
    if s.cost > c[s.pos] { continue }
    for _,e := range g[s.pos] {
      if c[e] > s.cost+1 {
        c[e] = s.cost+1
        heap.Push(&pq,&Item{e,s.cost+1})
      }
    }
  }
  return c
}
type Item struct { pos,cost int }
type PriorityQueue []*Item
func(pq PriorityQueue) Len() int { return len(pq) }
func(pq PriorityQueue) Less(i,j int) bool { return pq[i].cost < pq[j].cost }
func(pq PriorityQueue) Swap(i,j int) { pq[i],pq[j] = pq[j],pq[i] }
func(pq *PriorityQueue) Push(x interface{}) {
  item := x.(*Item)
  *pq = append(*pq,item)
}
func(pq *PriorityQueue) Pop() interface{} {
  old := *pq
  n := len(old)
  item := old[n-1]
  *pq = old[0:n-1]
  return item
}