using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
static class Program{
	const int mod=1000000007;
	static readonly int[][] di=new int[][]{new int[]{-1,0},new int[]{1,0},new int[]{0,-1},new int[]{0,1}};
	static void Main(){
		Sc sc=new Sc();
		int[] s=sc.Ia;int h=s[0],w=s[1];
		string[] bm=new string[h+2];
		bool[][] b=new bool[h+2][];
		var pq=new Pq<Mo<int[]>>(64,true);
		var pq2=new Pq<Mo<int[]>>(64,true);
		for(int i = 1;i<=h;i++) {
			bm[i]="X"+sc.S+"X";
			b[i]=new bool[w+2];
			for(int j = 1;j<=w;j++) {
				if(bm[i][j]=='S'){
					pq.Push(new Mo<int[]>(0,new int[]{i,j}));
				}
			}
		}
		bm[0]=new string('X',w+2);
		bm[h+1]=new string('X',w+2);
		int ans=int.MaxValue;
		while(pq.cnt>0&&pq.Top.n<=s[2]){
			Mo<int[]> q=pq.Top;
			pq.Pop();
			ans=Math.Min(ans,q.d[0]-1);
			ans=Math.Min(ans,q.d[1]-1);
			ans=Math.Min(ans,h-q.d[0]);
			ans=Math.Min(ans,w-q.d[1]);
			for(int i = 0;i<4;i++) {
				if(bm[q.d[0]+di[i][0]][q.d[1]+di[i][1]]=='.'&&!b[q.d[0]+di[i][0]][q.d[1]+di[i][1]]){
					b[q.d[0]][q.d[1]]=true;
					pq.Push(new Mo<int[]>(q.n+1,new int[]{q.d[0]+di[i][0],q.d[1]+di[i][1]}));
				}
			}
		}
		Console.WriteLine("{0}",(ans/s[2]+(ans%s[2]!=0?1:0)+1));
	}
}
public class Mo<T>:IComparable{
	public int n;
	public T d;
	public Mo(int n,T d){this.n=n;this.d=d;}
	public int CompareTo(object obj){
		Mo<T> mymo=(Mo<T>)obj;
		if(mymo.n>n){return -1;}
		else if(mymo.n<n){return 1;}
		else{return 0;}
	}
}
public class Pq<T> where T:IComparable{
	private T[] he;
	public int cnt=0,max=0;
	private Func<T,T,int> compare;
	public Pq(int max,bool mm){
		this.max=max;
		he=new T[max];
		if(mm){compare=Ao;}
		else{compare=Do;}
	}
	public void Push(T x){
		if(cnt==max){Extend();}
		int j=cnt;
		while(j!=0&&compare(x,he[(j-1)/2])>0){he[j]=he[(j-1)/2];j=(j-1)/2;}
		he[j]=x;
		cnt++;
	}
	public void Pop(){
		cnt--;
		T r=he[cnt];
		int j=0;
		while(true){
			if(j*2+1<cnt){
				if(compare(he[j*2+1],he[j*2+2])>0){j=j*2+1;}
				else{j=j*2+2;}
			}
			else if(j*2<cnt){j=j*2+1;}
			else{break;}
			if(compare(he[j],r)<=0){j=(j-1)/2;break;}
			he[(j-1)/2]=he[j];
		}
		he[j]=r;
	}
	private int Ao(T x,T y){return y.CompareTo(x);}
	private int Do(T x,T y){return x.CompareTo(y);}
	public T Top{get{return he[0];}}
	private void Extend(){
		T[] nhe=new T[max<<1];
		Array.Copy(he,nhe,max);
		he=nhe;
		max<<=1;
	}
}
public class Sc{
	private string[] a;
	private int n=-1,m=0;
	public int I{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return int.Parse(a[n]);}}
	public long L{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return long.Parse(a[n]);}}
	public double D{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return double.Parse(a[n]);}}
	public string S{get{if(n==-1||n==a.Length-1){n=-1;a=Console.ReadLine().Split();}n++;return a[n];}}
	public int[] Ia{get{if(n!=-1&&n!=a.Length-1){m=n;n=-1;return Array.ConvertAll(a.Skip(m+1).ToArray(),int.Parse);}return Array.ConvertAll(Console.ReadLine().Split(),int.Parse);}}
	public int[] Ia2{get{return Array.ConvertAll(("0 "+Console.ReadLine()+" 0").Split(),int.Parse);}}
	public long[] La{get{return Array.ConvertAll(Console.ReadLine().Split(),long.Parse);}}
	public long[] La2{get{return Array.ConvertAll(("0 "+Console.ReadLine()).Split(),long.Parse);}}
	public double[] Da{get{return Array.ConvertAll(Console.ReadLine().Split(),double.Parse);}}
	public string[] Sa{get{return Console.ReadLine().Split();}}
}