using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		long mod = 1000000;
		long Inf = (long) 1e18;
		long[][] min = new long[H][];
		bool[][] used = new bool[H][];
		for(int i=0;i<H;i++){
			min[i] = new long[W];
			used[i] = new bool[W];
			for(int j=0;j<W;j++) min[i][j] = Inf;
		}
		
		var SH = new SkewHeap<Pair>();
		int r0 = 0, c0 = 0;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(S[i][j] == 'S'){
					r0 = i; c0 = j;
				}
			}
		}
		
		SH.Push(new Pair(enc(r0,c0),0));
		min[r0][c0] = 0;
		while(SH.Count>0){
			var p = SH.Top; SH.Pop();
			int rc = p.Pos;
			int r = decR(rc);
			int c = decC(rc);
			long cost = p.Cost;
			if(used[decR(rc)][decC(rc)]) break;
			min[r][c] = cost;
			used[r][c] = true;
			for(int t=0;t<4;t++){
				int nr = r + dy[t];
				int nc = c + dx[t];
				if(!InRange(nr,0,H) || !InRange(nc,0,W)) continue;
				long ncost = cost;
				if(S[nr][nc] == '.') ncost ++;
				if(S[nr][nc] == '#') ncost += mod + 1;
				if(min[nr][nc] > ncost){
					min[nr][nc] = ncost;
					SH.Push(new Pair(enc(nr,nc),ncost));
				}
			}
		}
		
		//for(int i=0;i<H;i++) Console.WriteLine(String.Join(" ",min[i]));
		
		long ans = Inf;
		for(int i=0;i<H;i++){
			for(int j=0;j<W;j++){
				if(InRange(i,1,H-1) && InRange(j,1,W-1)) continue;
				long nes = min[i][j] + 1;
				long toho = nes % mod;
				long maho = nes / mod;
				long turn = 0;
				if(maho > 0) turn += (maho-1) / K + 1;
				long ntoho = (toho-1) / K + 1;
				if(turn < ntoho) turn += ntoho - turn; 
				//Console.WriteLine("{0} {1},ma;{2}, to:{3}, {4}",i,j,maho,toho,turn);
				ans = Math.Min(ans,turn);
			}
		}
		Console.WriteLine(ans);
		
		
	}
	
	int[] dy = new int[] {0, 1, 0, -1};
	int[] dx = new int[] {1, 0, -1, 0};
	bool InRange(int t, int l, int r){
		return l <= t && t < r;
	}
	int enc(int r, int c){ return (r*1024) | c;}
	int decR(int rc){ return rc / 1024;}
	int decC(int rc){ return rc % 1024;}
	
	
	int H,W;
	long K;
	String[] S;
	public Sol(){
		var d = ria();
		H = d[0]; W = d[1]; K = d[2];
		S = new String[H];
		for(int i=0;i<H;i++) S[i] = rs();
		
		
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class Pair : IComparable<Pair> {
	public int Pos;
	public long Cost;
	public Pair(int p, long c) {
		Pos = p; Cost = c;
	}
	public int CompareTo(Pair t) {
		//return this.Cost > t.Cost ? -1 : this.Cost < t.Cost ? 1 : 0;
		return this.Cost > t.Cost ? 1 : this.Cost < t.Cost ? -1 : 0;
	}
}


class SkewHeap<T> where T:IComparable<T>{
	public int Count{
		get{return cnt;}
		private set{cnt=value;}
	}
	
	public SkewHeap(){
		root=null;
		this.Count=0;
	}
	
	public void Push(T v){
		NodeSH<T> p=new NodeSH<T>(v);
		root=NodeSH<T>.Meld(root,p);
		this.Count++;
	}
	
	public void Pop(){
		if(root==null)return;
		root=NodeSH<T>.Meld(root.L,root.R);
		this.Count--;
	}
	
	public T Top{
		get{return root.Val;}
	}
	
	int cnt;
	NodeSH<T> root;
	
	class NodeSH<S> where S : IComparable<S> {
		public NodeSH<S> L, R;
		public S Val;

		public NodeSH(S v){
			Val = v;
			L = null; R = null;
		}
		public static NodeSH<S> Meld(NodeSH<S> a, NodeSH<S> b){
			if(a == null)return b;
			if (b == null) return a;
			if (a.Val.CompareTo(b.Val) > 0) swap(ref a, ref b);
			a.R = Meld(a.R, b);
			swap(ref a.L, ref a.R);
			return a;
		}

		static void swap<U>(ref U x, ref U y) {
			U t = x; x = y; y = t;
		}
	}
}

