def check
	if @now&@edge > 0
		p @cnt
		exit
	end
end

def move(m)
	@k.times{|x|
		@now|=(@now << 1|@now >> 1|@now<< @w|@now >> @w)&m
	}
end

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
@edge = ~((((x=(y=1<< @w*~-~-h)/~-(1<< @w))|x<< @w-1|x<< 1)^~-y)<< @w)

@cnt=1
405.times{
	move(mask)
	check
	@cnt+=1
	move(-1)
	check
}
