Problem:
Score : 700 points

Problem Statement

Takahashi is locked within a building.

This building consists of H×W rooms, arranged in H rows and W columns.
We will denote the room at the i-th row and j-th column as (i,j). The state of this room is represented by a character A_{i,j}. If A_{i,j}= #, the room is locked and cannot be entered; if A_{i,j}= ., the room is not locked and can be freely entered.
Takahashi is currently at the room where A_{i,j}= S, which can also be freely entered.

Each room in the 1-st row, 1-st column, H-th row or W-th column, has an exit.
Each of the other rooms (i,j) is connected to four rooms: (i-1,j), (i+1,j), (i,j-1) and (i,j+1).

Takahashi will use his magic to get out of the building. In one cast, he can do the following:

Move to an adjacent room at most K times, possibly zero. Here, locked rooms cannot be entered.

Then, select and unlock at most K locked rooms, possibly zero. Those rooms will remain unlocked from then on.

His objective is to reach a room with an exit. Find the minimum necessary number of casts to do so.

It is guaranteed that Takahashi is initially at a room without an exit.

Constraints

3 ≤ H ≤ 800

3 ≤ W ≤ 800

1 ≤ K ≤ H×W

Each A_{i,j} is # , . or S.

There uniquely exists (i,j) such that A_{i,j}= S, and it satisfies 2 ≤ i ≤ H-1 and 2 ≤ j ≤ W-1.

Input

Input is given from Standard Input in the following format:

H W K
A_{1,1}A_{1,2}...A_{1,W}
:
A_{H,1}A_{H,2}...A_{H,W}

Output

Print the minimum necessary number of casts.

Sample Input 1

3 3 3
#.#
#S.
###

Sample Output 1

1

Takahashi can move to room (1,2) in one cast.

Sample Input 2

3 3 3
###
#S#
###

Sample Output 2

2

Takahashi cannot move in the first cast, but can unlock room (1,2).
Then, he can move to room (1,2) in the next cast, achieving the objective in two casts.

Sample Input 3

7 7 2
#######
#######
##...##
###S###
##.#.##
###.###
#######

Sample Output 3

2

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		for (; solver.hasNext();) {
			solver.readBody(in.readLine());
		}
		solver.solve();
	}
}

class Solver {
	private class Point {
		int X;
		int Y;

		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + getOuterType().hashCode();
			result = prime * result + X;
			result = prime * result + Y;
			return result;
		}

		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			Point other = (Point) obj;
			if (!getOuterType().equals(other.getOuterType()))
				return false;
			if (X != other.X)
				return false;
			if (Y != other.Y)
				return false;
			return true;
		}

		public Point(int x, int y) {
			super();
			X = x;
			Y = y;
		}

		private Solver getOuterType() {
			return Solver.this;
		}
	}

	int H;
	int W;
	int K;
	int cnt;
	boolean[][] Room;
	Point Start;

	public void init() {
		H = 0;
		W = 0;
		K = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split(" ");
		H = Integer.parseInt(strArr[0]);
		W = Integer.parseInt(strArr[1]);
		K = Integer.parseInt(strArr[2]);
		Room = new boolean[H + 1][W + 1];
	}

	public boolean hasNext() {
		return cnt < H;
	}

	public void readBody(String str) {
		// System.out.println(str);
		// String[] strArr = str.split(" ");
		cnt++;
		for (int i = 0; i < W; i++) {
			Room[cnt][i + 1] = (str.charAt(i) == '.' || str.charAt(i) == 'S');
			if (str.charAt(i) == 'S')
				Start = new Point(i + 1, cnt);
		}
	}

	public void solve() {
		footPrint = new HashSet<Point>();
		footPrint.add(Start);
		// print();
		updFootPrint(null, Start, K);
		// System.out.println();
		// print();
		int min = Integer.MAX_VALUE;
		for (Point p : footPrint) {
			min = Integer.min(min, p.X - 1);
			min = Integer.min(min, W - p.X);
			min = Integer.min(min, p.Y - 1);
			min = Integer.min(min, H - p.Y);
		}
		int ans = min / K + 1;
		if (min % K != 0)
			ans++;
		System.out.println(ans);
	}

	HashSet<Point> footPrint;

	private void updFootPrint(Point prev, Point curr, int step) {
		for (int dy = -1; dy <= 1; dy++) {
			for (int dx = (dy == 0 ? -1 : 0); dx <= 1; dx += 2) {
				int x = curr.X + dx;
				int y = curr.Y + dy;
				if (prev != null && prev.X == x && prev.Y == y)
					continue;
				if (x < 1 || y < 1 || x > W || y > H)
					continue;
				if (!Room[y][x])
					continue;
				if (x == 1 || y == 1 || x == W || y == H) {
					footPrint.add(new Point(x, y));
				} else if (step <= 1) {
					footPrint.add(new Point(x, y));
				}
				updFootPrint(curr, new Point(x, y), step - 1);
			}
		}
	}

	private void print() {
		for (int y = 1; y <= H; y++) {
			for (int x = 1; x <= W; x++) {
				// if (Start.X == x && Start.Y == y) {
				// System.out.print("S");
				// continue;
				// }
				if (footPrint.contains(new Point(x, y))) {
					System.out.print("*");
					continue;
				}
				System.out.print(Room[y][x] ? "□" : "■");
			}
			System.out.println();
		}
	}
}