import std.stdio;
import std.conv, std.array, std.algorithm, std.string;
import std.math, std.random, std.range, std.datetime;
import std.bigint;
string read(){ static string[] ss; while(!ss.length) ss = readln.chomp.split; string res = ss[0]; ss.popFront; return res; }

class Cell{
	char c;
	int i, j;
	int _value;
	bool isVisited = false;
	Cell[] adjCells;
	this(char c, int i, int j){
		this.c = c;
		this.i = i;
		this.j = j;
		}
	int value(int x){
		this._value = x;
		this.isVisited = true;
		return this._value;
		}
	int value(){
		return this._value;
		}
	void add(Cell ce){
		this.adjCells ~= ce;
		ce.adjCells ~= this;
		}
	void runUntil(int k){
		if(k > 0){
			foreach(ce; this.adjCells){
				if(ce.c == '.' && (!ce.isVisited || ce.value > this.value + 1)){
					ce.value = this.value + 1;
					ce.runUntil(k - 1);
					}
				}
			}
		}
	}

void main(){
	int h = read.to!int;
	int w = read.to!int;
	int k = read.to!int;
	
	Cell[][] cells;
	foreach(i; 0 .. h){
		char[] cs = readln.chomp.to!(char[]);
		Cell[] xs;
		cells ~= xs;
		foreach(j, c; cs){
			Cell x = new Cell(c, i, cast(int) j);
			cells[i] ~= x;
			if(i > 0) x.add(cells[i - 1][j]);
			if(j > 0) x.add(cells[i][j - 1]);
			}
		}
	
	foreach(i; 0 .. h) foreach(j; 0 .. w) if(cells[i][j].c == 'S'){
		cells[i][j].value = 0;
		cells[i][j].runUntil(k);
		}
	
	debug writeln("k=", k);
	debug foreach(i; 0 .. h) cells[i].map!(x => x.isVisited? '*': x.c).writeln;
	
	
	int minToWall = 1000;
	foreach(i; 0 .. h) foreach(j; 0 .. w) if(cells[i][j].isVisited){
		int t = min(i, h - 1 - i, j, w - 1 - j);
		if(t < minToWall) minToWall = t;
		}
	
	debug writeln("minToWall: ", minToWall);
	
	int ans = 1 + (minToWall + (k - 1)) / k;
	
	ans.writeln;
	
	
	}