#include<iostream>
#include<map>
using namespace std;

int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a/gcd(a,b)*b;}

int main(){
  int h,w;
  string g[10];
  int dx[] = {0,1,1,1,0,-1,-1,-1},dy[] ={-1,-1,0,1,1,1,0,-1};
  int lm;

  while(cin>> h >> w &&(h||w)){
    for(int i=0;i<h;i++)cin >> g[i];

    map<string,int> m;
    for(int i=0;i<h;i++)
      for(int j=0;j<w;j++)
	for(int k=0;k<8;k++){
	  string tmp;
	  tmp += g[i][j];
	  int sx = j,sy = i;
	  if(k&1)lm = lcm(h,w);
	  else if(!(k%4))lm = h;
	  else lm = w;
	  for(int t=1;t<lm;t++){
	    sx = (sx+w+dx[k])%w;
	    sy = (sy+h+dy[k])%h;
	    tmp += g[sy][sx];
	    m[tmp]++;
	  }
	}


    string ans = "A";
    map<string,int>::iterator ite;
    for(ite=m.begin();ite!=m.end();ite++)
      if((*ite).second>1){
	string tmp = (*ite).first;
	if(ans.size()<tmp.size() || (ans.size()==tmp.size()&&ans>tmp))ans = tmp;
      }
    if(ans.size()>1)cout << ans << endl;
    else cout << 0 << endl;
  }
}