#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;

class Egalitarianism { public:
	int maxDifference(vector <string> isFriend, int d)
	{
		int N = isFriend.size();
		int INF = 0x3fffffff;
		vector< vector<int> > g(N, vector<int>(N, INF));
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
			if(x==y)
				g[x][y] = 0;
			else if(isFriend[x][y]=='Y')
				g[x][y] = 1;

		for(int k=0; k<N; ++k)
		for(int i=0; i<N; ++i)
		for(int j=0; j<N; ++j)
			g[i][j] = min(g[i][j], g[i][k]+g[k][j]);

		int maxd = 0;
		for(int x=0; x<N; ++x)
		for(int y=0; y<N; ++y)
			maxd = max(maxd, g[x][y]);
		return maxd==INF ? -1  : maxd*d;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, Egalitarianism().maxDifference(isFriend, d));}
int main(){

CASE(0)
	string isFriend_[] = {"NYN",
 "YNY",
 "NYN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 10; 
	int _ = 20; 
END
CASE(1)
	string isFriend_[] = {"NN",
 "NN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 1; 
	int _ = -1; 
END
CASE(2)
	string isFriend_[] = {"NNYNNN",
 "NNYNNN",
 "YYNYNN",
 "NNYNYY",
 "NNNYNN",
 "NNNYNN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 1000; 
	int _ = 3000; 
END
CASE(3)
	string isFriend_[] = {"NNYN",
 "NNNY",
 "YNNN",
 "NYNN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 584; 
	int _ = -1; 
END
CASE(4)
	string isFriend_[] = {"NYNYYYN",
 "YNNYYYN",
 "NNNNYNN",
 "YYNNYYN",
 "YYYYNNN",
 "YYNYNNY",
 "NNNNNYN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 5; 
	int _ = 20; 
END
CASE(5)
	string isFriend_[] = {"NYYNNNNYYYYNNNN",
 "YNNNYNNNNNNYYNN",
 "YNNYNYNNNNYNNNN",
 "NNYNNYNNNNNNNNN",
 "NYNNNNYNNYNNNNN",
 "NNYYNNYNNYNNNYN",
 "NNNNYYNNYNNNNNN",
 "YNNNNNNNNNYNNNN",
 "YNNNNNYNNNNNYNN",
 "YNNNYYNNNNNNNNY",
 "YNYNNNNYNNNNNNN",
 "NYNNNNNNNNNNNNY",
 "NYNNNNNNYNNNNYN",
 "NNNNNYNNNNNNYNN",
 "NNNNNNNNNYNYNNN"}
;
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 747; 
	int _ = 2988; 
END
/*
CASE(6)
	string isFriend_[] = {"NY",
 "YN"};
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = 0; 
	int _ = 0; 
END
CASE(7)
	string isFriend_[] = ;
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = ; 
	int _ = ; 
END
CASE(8)
	string isFriend_[] = ;
	  vector <string> isFriend(isFriend_, isFriend_+sizeof(isFriend_)/sizeof(*isFriend_)); 
	int d = ; 
	int _ = ; 
END
*/
}
// END CUT HERE
