#include using namespace std; const int MAXN = 105; const int INF = 0x3f3f3f3f; int n; int graph[MAXN][MAXN]; int dist[MAXN]; // BFS?????? int bfs(int start, int end) { // ??????? memset(dist, 0x3f, sizeof(dist)); queue q; dist[start] = 0; q.push(start); while (!q.empty()) { int u = q.front(); q.pop(); // ???????? if (u == end) { return dist[u]; } // ?????? for (int v = 0; v < n; v++) { // ??u?v????v???? if (graph[u][v] == 1 && dist[v] == INF) { dist[v] = dist[u] + 1; q.push(v); } } } // ???? return -1; } int main() { cin >> n; // ?????? for (int i = 0; i < n; i++) { string line; cin >> line; for (int j = 0; j < n; j++) { graph[i][j] = (line[j] == 'Y') ? 1 : 0; } } // ??A?B????? if (graph[0][1] == 1) { cout << 0 << endl; return 0; } // BFS???A?B?????????????? int result = bfs(0, 1); cout << result << endl; return 0; }