def calcs(x,y,ax,ay,bx,by) ((ax-x)*(by-y)-(bx-x)*(ay-y))/2.0 end
def inpf() a=gets.chomp.split(" ").map(&:to_f)end
def inps() a=gets.chomp.split(" ")end  
def copy(a) Marshal.load(Marshal.dump(a)) end
def kaizyo(n)(n < 2)? 1 : (2..n).inject(:*) end
def scount(a) b=na(a.max+1);a.each{|n|b[n]+=1};return b end
def na(n=0,d=0) Array.new(n,d)end
def na2(n=0,m=0,d=0) Array.new(n){Array.new(m,d)}end
def na3(n=0,m=0,l=0,d=0) Array.new(n){Array.new(m){Array.new(l,d)}}end
def bit(n) n.to_s(2).split("").map(&:to_i) end
def inp() a=gets.chomp.split(" ").map(&:to_i)end
h,w,k=inp
d=[]
x=y=0
h.times do |i|
  dd=inps[0]
  if(dd.include?("S"))
    y=i
    x=dd.index("S")
  end
  d[i] = dd
end
que = [[y,x,k]]
min = 9999999
memo = na2(h+100,w+100)
while(que.size > 0)
  dd = que.shift
  x,y = dd[1],dd[0]
  break if(dd[2] == 0)
  if(x < 0 or y < 0 or x >= w or y>=h)
    puts popopopoop
    exit
  end
  next if(memo[y][x] == 1)
  next if(d[y][x] == "#")
  min = [x,y,h-y-1,w-x-1,min].min
  que.push([y-1,x,dd[2]-1])
  que.push([y+1,x,dd[2]-1])
  que.push([y,x+1,dd[2]-1])
  que.push([y,x-1,dd[2]-1])
  memo[y][x] = 1
end
puts 1+(min+k-1)/k
=begin
=end