def Solve():
	H, W, K = [int(i) for i in input().split()]
	S = (0,0)
	A = [[0] * W for i in range(H)]
	for iy in range(H):
		a = input()
		for ix in range(W):
			if a[ix] == "S":
				S = (ix, iy)
				A[iy][ix] = 0
			elif a[ix] == "#":
				A[iy][ix] = 1
			elif a[ix] == ".":
				A[iy][ix] = 0
	def recursive(p):
		for c in [(p[0] + 1, p[1]),(p[0] - 1, p[1]),(p[0], p[1] + 1),(p[0], p[1] - 1),]:
			if A[c[0]][c[1]] == 0:
				if 0 < c[0] < H - 1 and 0 < c[1] < W - 1:
					A[c[0]][c[1]] = -1
					if recursive(c):
						return True
				else:
					return True
		return False
	if recursive(S):
		print(1)
	else:
		print(2)
Solve()