using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CS_AtCoder {
	class Program {
		static void Main(string[] args) {
			new Calc().Solve();
		}
	}
	public class Calc {
		Scan cin = new Scan();
		int[] HWK;
		List<string> box;
		public void Solve() {
			HWK = cin.ReadInt(' ');
			box = new List<string>();
			int sx = 0, sy = 0;
			for (int i = 0; i < HWK[0]; i++) {
				var str = cin.ReadLine();
				if (str.Contains('S')) { sy = i;sx = str.IndexOf('S'); }
				box.Add(str);
			}
			search(sx, sy, 0);
			var pos = position.Distinct();
			List<int> list = new List<int>();
			foreach (var item in pos) {
				sx = item.Item1;
				sy = item.Item2;
				var idxBox = box.Select((v, idx) => new { V = v, I = idx });
				var up = idxBox.Where(_ => _.I <= sy).Select(_ => _.V[sx]).Count(_ => _ == '#');
				var down = idxBox.Where(_ => _.I >= sy).Select(_ => _.V[sx]).Count(_ => _ == '#');
				var left = idxBox.ElementAt(sy).V.Substring(sx, HWK[1] - sx).Count(_ => _ == '#');
				var right = idxBox.ElementAt(sy).V.Substring(0, sx + 1).Count(_ => _ == '#');

				var ranges = new int[4] { up, down, left, right };
				list.Add(ranges.Min());
			}


			int cnt = 1;
			int min = list.Min();
			while (min>0) {
				min -= HWK[2];
				cnt++;
			}
			cnt.WL();

			return;
		}
		List<Tuple<int, int>> position = new List<Tuple<int, int>>();
		private void search(int x, int y, int depth) {
			position.Add(new Tuple<int, int>(x, y));
			if (depth == HWK[2]) return;
			if (x + 1 < HWK[1] && box[y][x + 1] != '#') search(x + 1, y, depth + 1);
			if (x - 1 >= 0 && box[y][x - 1] != '#') search(x - 1, y, depth + 1);
			if (y + 1 < HWK[0] && box[y + 1][x] != '#') search(x, y + 1, depth + 1);
			if (y - 1 >= 0 && box[y - 1][x] != '#') search(x, y - 1, depth + 1);
		}
	}

	public class Scan {
		public Scan() { }
		public int ReadInt() => int.Parse(Console.ReadLine());
		public int[] ReadInt(char separator) => Console.ReadLine().Split(separator).Select(int.Parse).ToArray();
		public double ReadDouble() => double.Parse(Console.ReadLine());
		public double[] ReadDouble(char separator) => Console.ReadLine().Split(separator).Select(double.Parse).ToArray();
		public long ReadLong() => long.Parse(Console.ReadLine());
		public long[] ReadLong(char separator) => Console.ReadLine().Split(separator).Select(long.Parse).ToArray();
		public string ReadLine() => Console.ReadLine();
		public string[] ReadLine(char separator) => Console.ReadLine().Split(separator).ToArray();
	}
	public static class Utils {
		public static string Docking<T>(this IEnumerable<T> collection,Func<T,string> filter = null) {
			StringBuilder sb = new StringBuilder();
			foreach (var item in collection) {
				sb.Append(filter != null ? filter(item) : item.ToString());
			}
			return sb.ToString();
		}
		public static void WL(this object obj) => Console.WriteLine(obj);
		public static int IntParse(this string s) => int.Parse(s);
		public static int IntParse(this char c) => (c + "").IntParse();
	}
}
