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

bool is_ps( LL v )
{
	LL sv = (LL)sqrt( double(v) );
	return (sv-1)*(sv-1) == v
	    || sv*sv == v
	    || (sv+1)*(sv+1) == v;
}

class FindingSquareInTable {
public:
	int findMaximalSquare(vector <string> table) 
	{
		LL ans = -1;

		int Y = table.size();
		int X = table[0].size();
		for(int y=0; y<Y; ++y)
			for(int x=0; x<X; ++x)
				for(int dy=-Y; dy<Y; ++dy)
					for(int dx=-X; dx<X; ++dx)
						if(dy||dx)
						{
							LL v = 0;
							for(int i=0;; ++i)
							{
								int yy = y+dy*i;
								int xx = x+dx*i;
								if( xx<0 || X<=xx || yy<0 || Y<=yy )
									break;
								int d = table[yy][xx]-'0';
								v = v*10 + d;
								if( is_ps(v) )
									ans = max(v, ans);
							}
						}
		return ans;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	string table_[] = {"123",
 "456"};
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = 64; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }
int Test_(Case_<1>) {
	string table_[] = {"00000",
 "00000",
 "00200",
 "00000",
 "00000"};
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = 0; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }
int Test_(Case_<2>) {
	string table_[] = {"3791178",
 "1283252",
 "4103617",
 "8233494",
 "8725572",
 "2937261"};
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = 320356; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }
int Test_(Case_<3>) {
	string table_[] = {"135791357",
 "357913579",
 "579135791",
 "791357913",
 "913579135"}
;
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = 9; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }
int Test_(Case_<4>) {
	string table_[] = {"553333733",
 "775337775",
 "777537775",
 "777357333",
 "755553557",
 "355533335",
 "373773573",
 "337373777",
 "775557777"};
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = -1; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }
int Test_(Case_<5>) {
	string table_[] = {"257240281",
 "197510846",
 "014345401",
 "035562575",
 "974935632",
 "865865933",
 "684684987",
 "768934659",
 "287493867"};
	  vector <string> table(table_, table_+sizeof(table_)/sizeof(*table_)); 
	int RetVal = 95481; 
	return verify_case(RetVal, FindingSquareInTable().findMaximalSquare(table)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE

