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 C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;

namespace Codeforces
{
	internal class Template
	{
		private void Solve()
		{
			var h = cin.NextInt();
			var w = cin.NextInt();
			var k = cin.NextInt();
			var map = new string[h];
			var start = new Point(0, 0);
			for (var i = 0; i < h; i++)
			{
				map[i] = cin.NextString();
				var idx = map[i].IndexOf("S");
				if (idx >= 0)
				{
					start.Y = i;
					start.X = idx;
				}
			}
			if (start.X == 0 || start.X == w - 1 || start.Y == 0 || start.Y == h - 1)
			{
				Console.WriteLine(0);
				return;
			}
			var visited = new bool[h, w];
			var dx = new[] {-1, 0, 1, 0};
			var dy = new[] {0, -1, 0, 1};
			var queue = new Queue<Tuple<Point, int>>();
			queue.Enqueue(Tuple.Create(start, 0));
			while (queue.Any())
			{
				var front = queue.Dequeue();
				var p = front.Item1;
				var step = front.Item2;
				if (visited[p.Y, p.X])
				{
					continue;;
				}
				visited[p.Y, p.X] = true;
				if (step == k)
				{
					continue;
				}
				for (var i = 0; i < dx.Length; i++)
				{
					var x = p.X + dx[i];
					var y = p.Y + dy[i];
					if (x >= 0 && x < w && y >= 0 && y < h && map[y][x] != '#')
					{
						queue.Enqueue(Tuple.Create(new Point(x, y), step + 1));
					}
				}
			}
			var best = int.MaxValue;
			for (var y = 0; y < h; y++)
			{
				for (var x = 0; x < w; x++)
				{
					if (visited[y, x])
					{
						var xl = (x+(k-1))/k;
						var xr = (((w - 1) - x) + (k - 1))/k;
						var yl = (y + (k - 1)) / k;
						var yr = (((h - 1) - y) + (k - 1)) / k;
						best = Math.Min(best, Math.Min(Math.Min(xl, xr), Math.Min(yl, yr)));
					}
				}
			}
			Console.WriteLine(best + 1);
		}

		public class Point
		{
			public int X, Y;

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

		private static readonly Scanner cin = new Scanner();

		private static void Main()
		{
#if DEBUG
			var inputText = File.ReadAllText(@"..\..\input.txt");
			var testCases = inputText.Split(new[] { "input" }, StringSplitOptions.RemoveEmptyEntries);
			var consoleOut = Console.Out;
			for (var i = 0; i < testCases.Length; i++)
			{
				var parts = testCases[i].Split(new[] { "output" }, StringSplitOptions.RemoveEmptyEntries);
				Console.SetIn(new StringReader(parts[0].Trim()));
				var stringWriter = new StringWriter();
				Console.SetOut(stringWriter);
				var sw = Stopwatch.StartNew();
				new Template().Solve();
				sw.Stop();
				var output = stringWriter.ToString();

				Console.SetOut(consoleOut);
				var color = ConsoleColor.Green;
				var status = "Passed";
				if (parts[1].Trim() != output.Trim())
				{
					color = ConsoleColor.Red;
					status = "Failed";
				}
				Console.ForegroundColor = color;
				Console.WriteLine("Test {0} {1} in {2}ms", i + 1, status, sw.ElapsedMilliseconds);
			}
			Console.ReadLine();
			Console.ReadKey();
#else
			new Template().Solve();
			Console.ReadLine();
#endif
		}
	}

	internal class Scanner
	{
		private string[] s = new string[0];
		private int i;
		private readonly char[] cs = { ' ' };

		public string NextString()
		{
			if (i < s.Length) return s[i++];
			var line = Console.ReadLine() ?? string.Empty;
			s = line.Split(cs, StringSplitOptions.RemoveEmptyEntries);
			i = 1;
			return s.First();
		}

		public double NextDouble()
		{
			return double.Parse(NextString());
		}

		public int NextInt()
		{
			return int.Parse(NextString());
		}

		public long NextLong()
		{
			return long.Parse(NextString());
		}
	}
}