#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char A[810][810] = {0};
int H, W, K, M = 1, x, y;

int move(int att) {
	if (!att || !check()) return 1;
	else if (A[x-1][y] == '.' || A[x][y-1] == '.' || A[x+1][y] == '.' || A[x][y+1] == '.') {
		if (x <= y && x <= H-x && x <= W-y) x--;
		else if (y <= x && y <= H-x && y <= W-y) y--;
		else if (H-x <= x && H-x <= y && H-x <= W-y) x++;
		else if (W-y <= x && W-y <= y && W-y <= H-x) y++;
		return 0;
	} else return 1;
}

void change(int pt) {
	int i, p, q;

	if (x <= y && x <= H-x && x <= W-y) {
		q = y;
		for (i=0;i<pt||q>=0;i++) {
			A[x][q--] = '.';
		}
	} else if (y <= x && y <= H-x && y <= W-y) {
		p = x;
		for (i=0;i<pt||p>=0;i++) {
			A[p--][y] = '.';
		}
	} else if (H-x <= x && H-x <= y && H-x <= W-y) {
		q = y;
		for (i=0;i<pt||q<W;i++) {
			A[x][q++] = '.';
		}
	} else if (W-y <= x && W-y <= y && W-y <= H-x) {
		p = x;
		for (i=0;i<pt||p<H;i++) {
			A[p++][y] = '.';
		}
	}
}

int check() {
	if (x==0 || y==0 || x==H-1 || y==W-1) return 0;
	else return 1;
}

int main(void) {
	char *pos;
	int i, ret, res;
	scanf("%d%d%d", &H, &W, &K);
	for (i=0;i<H;i++) scanf("%s", &A[i]);
	pos = memchr(A, 'S', 810*H);
	x = (pos-A[0])/810;
	y = (pos-A[0])%810;
//	printf("%d %d\n", x, y);

	while (check()) {
		res = K;
		while (1) {
			ret = move(res);
//			printf("%d: %d %d\n", M, x, y);
			if (ret) break;
			else res--;
		}
		if (check()) {
			M++;
			change(K);
		} else break;
	}

	printf("%d\n", M);

	return 0;
}