// Arup Guha
// 3/17/2019
// Solution to 2019 MAPS Problem L: Safe Houses

import java.util.*;

public class safehouses {

	public static void main(String[] args) {
		
		// Read basic input.
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		
		// Will store where each spy and house is.
		ArrayList<Integer> spies = new ArrayList<Integer>();
		ArrayList<Integer> houses = new ArrayList<Integer>();
		
		// Read in the grid, storing spy and house locations.
		for (int i=0; i<n; i++) {
			String line = stdin.next();
			for (int j=0; j<n; j++) {
				if (line.charAt(j) == 'S') spies.add(i*n+j);
				if (line.charAt(j) == 'H') houses.add(i*n+j);
			}
		}
		
		int res = 0;
		
		// Go thorough each spy.
		for (int i=0; i<spies.size(); i++) {
			
			int best = 2*n;
			
			// Find closest house.
			for (int j=0; j<houses.size(); j++) {
				int sx = spies.get(i)/n;
				int sy = spies.get(i)%n;
				int hx = houses.get(j)/n;
				int hy = houses.get(j)%n;
				best = Math.min(best, Math.abs(sx-hx)+Math.abs(sy-hy));
			}
			
			// Update this to be the largest of these values for each spy.
			res = Math.max(res, best);
		}
		
		System.out.println(res);
	}	
}