io = STDIN
h,w,$k=io.gets.chomp.split.map(&:to_i)
ar=[]
ss=[]
j=0
h.times do |i|
  str = io.gets.chomp
  (ss=[i,j]) if j=str.index("S")
  ar << str.split(//).to_a
end
i,j=ss
hash1=Hash.new{|h,k|h[k]=0.0}
hash2=Hash.new{|h,k|h[k]=0.0}
hash3=Hash.new{|h,k|h[k]=0.0}
hash4=Hash.new{|h,k|h[k]=0.0}
(0..(i-1)).each {|ii|hash1[ar[ii][j]]+=1}
((i+1)...h).each {|ii|hash2[ar[ii][j]]+=1}
(0..(j-1)).each {|jj|hash3[ar[i][jj]]+=1}
((j+1)...w).each {|jj|hash4[ar[i][jj]]+=1}
ar=[hash1,hash2,hash3,hash4]
def calc(h)
  ss=(h["#"]/$k).ceil
  pp=(h["."]/$k).ceil
  if ss==0
    pp
  elsif pp==0
    ss+1
  else
    if pp>ss
      pp
    else
      ss+1
    end
  end
end
puts ar.map{|h|calc(h)}.min