H, W, K = gets.split.map(&:to_i)
A = H.times.map { gets.chomp.chars }

sy = -1
sx = -1

A.each_with_index do |row, y|
  row.each_with_index do |c, x|
    next if c != 'S'

    sy = y
    sx = x
  end
end

queue = []
queue << [sy, sx]

DY = [-1, 0, 1, 0]
DX = [0, 1, 0, -1]

visited = Hash.new { |h, k| h[k] = Hash.new }
min_dist = Float::INFINITY

until queue.empty?
  y, x = queue.shift

  next if visited[y][x]
  visited[y][x] = true

  min_dist = [min_dist, y, H - y - 1].min
  min_dist = [min_dist, x, W - x - 1].min

  DY.zip(DX) do |dy, dx|
    ny = y + dy
    nx = x + dx

    next if ny < 0 || nx < 0 || H <= ny || W <= nx
    next if A[ny][nx] == '#'

    queue << [ny, nx]
  end
end

if min_dist == 0
  puts 1
else
  puts 1 + Rational(min_dist, K).ceil
end
