package main

import (
  "bufio"
  "os"
  "strconv"
  "strings"
  "fmt"
  "container/heap"
)

var reader = bufio.NewReaderSize(os.Stdin, 1000000)
var writer = bufio.NewWriter(os.Stdout)
func NextLine() string {
  var line, buffer []byte
  var isPrefix bool = true
  var err error
  for isPrefix {
    line, isPrefix, err = reader.ReadLine()
    if err != nil { panic(err) }
    buffer = append(buffer, line...)
  }
  return string(buffer)
}
func NextIntVec() []int {
  L := strings.Split(NextLine(), " ")
  N := make([]int, len(L))
  for i := range N {
    N[i], _ = strconv.Atoi(L[i])
  }
  return N
}
func Write(s interface{}) {
  fmt.Fprintln(writer, s)
}
func Output() {
  _ = writer.Flush()
}

type Item struct {
  Day int
  Step int
  Break int
  x int
  y int
}
func less(a, b *Item) bool {
  if a.Day != b.Day {
    return a.Day < b.Day
  } else if a.Step != b.Step {
    return a.Step < b.Step
  } else {
    return a.Break < b.Break
  }
}
type PriorityQueue []*Item
func (pq PriorityQueue) Len() int { return len(pq) }
func (pq PriorityQueue) Less(i, j int) bool {
  return less(pq[i], pq[j])
}
func (pq PriorityQueue) Swap(i, j int) {
  pq[i], pq[j] = pq[j], pq[i]
}
func (pq *PriorityQueue) Pop() interface{} {
  old := *pq
  n := len(old)
  item := old[n - 1]
  *pq = old[0:n - 1]
  return item
}
func (pq *PriorityQueue) Push(x interface{}) {
  item := x.(*Item)
  *pq = append(*pq, item)
}

func drill(PQ *PriorityQueue, A *[]string, B *[][]Item, x, y, a, b, K int) {
  var C Item
  C = (*B)[x][y]
  C.x = a
  C.y = b
  if (*A)[a][b] == '.' {
    C.Step += 1
  } else {
    C.Break += 1
    if C.Day <= (C.Break - 1) / K + 1 {
      C.Day = (C.Break - 1) / K + 2
      C.Step = 1
    } else {
      C.Step += 1
    }
  }
  if C.Step > K {
    C.Day += 1
    C.Step -= K
  }
  if less(&C, &(*B)[a][b]) {
    (*B)[a][b] = C
    heap.Push(PQ, &C)
  }
}

func main() {
  HWK := NextIntVec()
  H := HWK[0]
  W := HWK[1]
  K := HWK[2]
  A := make([]string, H)
  B := make([][]Item, H)
  var PQ PriorityQueue
  for i := range A {
    A[i] = NextLine()
    B[i] = make([]Item, W)
    for j := range B[i] {
      B[i][j] = Item{H * W, 0, 0, i, j}
      if A[i][j] == 'S' {
        B[i][j].Day = 1
        heap.Push(&PQ, &B[i][j])
      }
    }
  }
  for {
    q := heap.Pop(&PQ).(*Item)
    if q.x == 0 || q.x == W - 1 || q.y == 0 || q.y == H - 1 {
      Write(q.Day)
      break
    }
    drill(&PQ, &A, &B, q.x, q.y, q.x - 1, q.y, K)
    drill(&PQ, &A, &B, q.x, q.y, q.x, q.y - 1, K)
    drill(&PQ, &A, &B, q.x, q.y, q.x + 1, q.y, K)
    drill(&PQ, &A, &B, q.x, q.y, q.x, q.y + 1, K)
  }
  Output()
}