class Egalitarianism { public: int maxDifference(vector <string> F, int D) { int N=F.size(); ll INF = 1LL<<50; VVI w(N, VI(N, INF)); REP(i, N) w[i][i] = 0; REP(i, N) REP(j, N) if(F[i][j]=='Y') w[i][j] = 1; REP(k, N) REP(i, N) REP(j, N) w[i][j]=min(w[i][j], w[i][k]+w[k][j]); ll d = 0; REP(i, N) REP(j, N) d=max(d, w[i][j]); return d==INF ? -1 : d*D; } };