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);
					}
				}
			}
		}
	}

X[] q;

class X{
	Cell cell;
	int value;
	this(Cell cell, int value){
		this.cell = cell;
		this.value = value;
		}
	void go(){
		cell.value = this.value;
		if(this.value > 0){
			foreach(ce; cell.adjCells){
				if(ce.c == '.' && !ce.isVisited){
					ce.isVisited = true;
					q ~= new X(ce, this.value - 1);
					}
				}
			}
		q = q[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'){
		q ~= new X(cells[i][j], k);
		}
	while(q.length){
		q[0].go();
		}
	
	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;
	
	
	}