h,w,k=gets.split.map &:to_i

w+=1
src=$<.read.to_s

mask=src.tr("S.#\n","110").to_i(2)
now =src.tr("S.#\n","10").to_i(2) 
tmp=((-(tmp=1 << w * h -w )/~-(1<< w))&~-tmp);tmp=((tmp&-tmp)+tmp)&tmp
edge = ~tmp;
edge|=edge >> 1|edge << 1
#puts sprintf("%0#{w*h+w+w}b",mask).scan(/.{#{w}}/),"~>",sprintf("%0#{w*h+w+w}b",edge).scan(/.{#{w}}/)
#return 0
cnt=1
8.times{
  k.times{|x|
    now|=( now << 1|now >> 1|now<< w|now >> w)&mask
  }
#  puts sprintf("%0#{w*(h+2)}b",now).scan(/.{#{w}}/),"=>", sprintf("%0#{w*(h+2)}b",mask).scan(/.{#{w}}/),""

  if(now&edge > 0)
    puts cnt
    exit(0)
  end
  tmp=now;
  k.times{|x|
    tmp=(tmp  << 1|tmp >> 1|tmp<< w|tmp >> w)
  }
  mask|=tmp
  cnt+=1
}
  
