using System;
using System.Linq;//リストの使用
using System.Collections.Generic;
class Program
{
static string[] input = Console.ReadLine().Split(' ');
static long r = long.Parse(input[0]);//行数
static long c = long.Parse(input[1]);//列数
static long k = long.Parse(input[2]);//初手移動可能マス数
static long[,] startPosition = new long[1,2];//スタート座標
static long[,] startPositionsSecond;//2回目のスタート座標
//static long[] goalPosition = new long[2];//ゴール座標
static long[,] distances = new long[r,c];//最短距離
static Queue<long[]> que = new Queue<long[]>();//幅優先探索用のキュー
static long[,] wallPositions = new long[r+2,c+2];//壁は1
static long[] VectorArrow = new long[8]{0,1,0,-1,1,0,-1,0};//上下左右に進む為のベクトル

	static void Main()
	{  
		for(long i = 0; i < r; i++)
    {
      string inputc = Console.ReadLine();
      for(long j = 0; j < c; j++)
      {
        if(inputc[(int)j] == 'S')//スタート地点の取得
        {
          startPosition[0,0] = i;
          startPosition[0,1] = j;
        }
        if(inputc[(int)j] == '#') wallPositions[i+1,j+1] = 1;//壁の取得
      }
    }

    for(long i = 0; i < r+2; i++)//周囲を壁で埋める
    {
      wallPositions[i,0] = 1;
      wallPositions[i,c+1] = 1;
    }
    for(long j = 0; j < c+2; j++)
    {
      wallPositions[0,j] = 1;
      wallPositions[r+1,j] = 1;
    }
    
		BFS(startPosition);//幅優先探索

    long startCount = 0;//開始地点の候補数
    for(long i = 0; i < r; i++)
    {
      for(long j = 0; j < c; j++)
      {
        if(distances[i,j] <= k) startCount++;
      }
    }

    startPositionsSecond = new long[startCount,2];//第2のスタート地点
    for(long i = 0; i < r; i++)
    {
      for(long j = 0; j < c; j++)
      {
        if(distances[i,j] <= k)
        {
          startPositionsSecond[startCount-1, 0] = i;
          startPositionsSecond[startCount-1, 1] = j;
          startCount--;
        }
      }
    }

    for(long i = 0; i < r; i++)
    {
      for(long j = 0; j < c; j++)
      {
        wallPositions[i+1,j+1] = 0;//周囲以外の壁を全て削除
      }
    }

    BFS(startPositionsSecond);//幅優先探索

    long answer = long.MaxValue;
    for(long i = 0; i < r; i++)
    {
      answer = Math.Min(answer, (distances[i,0]+(k-1))/k);
      answer = Math.Min(answer, (distances[i,c-1]+(k-1))/k);
    }
    for(long i = 0; i < c; i++)
    {
      answer = Math.Min(answer, (distances[0,i]+(k-1))/k);
      answer = Math.Min(answer, (distances[r-1,i]+(k-1))/k);
    }
    answer++;
    Console.WriteLine(answer);
	}

  static void BFS(long[,] startPositions)//幅優先探索。引数はスタートの座標。
  {
    int n = startPositions.GetLength(0);//スタート地点の個数
    long[][] startPosition = new long[n][];
    for(int i = 0; i < n; i++)
    {
      startPosition[i] = new long[2];
      startPosition[i][0] = startPositions[i,0];
      startPosition[i][1] = startPositions[i,1];
      que.Enqueue(startPosition[i]);
    }
    
    for(long i = 0; i < r; i++)
    {
      for(long j = 0; j < c; j++)
      {
        distances[i,j] = long.MaxValue;//距離を無限と仮定
      }
    }

    for(int i = 0; i < n; i++)
    {
      distances[startPosition[i][0], startPosition[i][1]] = 0;//スタート地点までの距離は0
    }
    long poppedMemoX = 0;
    long poppedMemoY = 0;
    while(que.Count > 0)
    {
      poppedMemoX = que.Peek()[0];
      poppedMemoY = que.Peek()[1];
      que.Dequeue();
    
      for(long partnerNum = 0; partnerNum < 4; partnerNum++)//上下左右に進みたい
      {
        long aimX = poppedMemoX+VectorArrow[2*partnerNum];
        long aimY = poppedMemoY+VectorArrow[2*partnerNum+1];
        if(wallPositions[aimX+1, aimY+1] == 1) continue;//壁なら無視
        if(distances[aimX, aimY] != long.MaxValue) continue;//距離が確定済みなら無視
        distances[aimX, aimY] = distances[poppedMemoX, poppedMemoY] + 1;//距離の加算
        que.Enqueue(new long[2]{aimX, aimY});
      }
    }

      /*
      for(long i = 0; i < r; i++)//各点までの距離の表示
      {
        string answer = "";
        for(long j = 0; j < c; j++)
        {
          if(wallPositions[i+1,j+1] == 1) answer += "#";
          else answer += distances[i,j].ToString();
        }
        Console.WriteLine(answer);
      }
      */
      
      //Console.WriteLine(distances[goalPosition[0], goalPosition[1]]);//ゴールまでの最短距離
  }
}

Measured memory_bytes: 83304