h, w, k=map(int, raw_input().split())
a=["#"*(w+2)]+["#"+raw_input()+"#" for i in range(h)]+["#"*(w+2)]
q=[]
first=0
dis=[[0]*(w+2) for i in range(h+2)]
for i in range(h+2):
	for j in range(w+2):
		if a[i][j]=="S":
			q.append((i, j))
			dis[i][j]=1
while first<len(q):
	cur_x, cur_y=q[first]
	first+=1
	for next_x, next_y in ((cur_x+1, cur_y), (cur_x-1, cur_y), (cur_x, cur_y+1), (cur_x+1, cur_y-1)):
		if a[next_x][next_y]=="." and not dis[next_x][next_y]:
			q.append((next_x, next_y))
			dis[next_x][next_y]=dis[cur_x][cur_y]+1
result=h*w
for i in range(h+2):
	for j in range(w+2):
		if 0<dis[i][j]<=k+1:
			dis_exit=min(i-1, j-1, h-i, w-j)
			cnt=(dis_exit+k-1)/k
			result=min(result, 1+cnt)
print result