#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
//#include <unordered_set>
#include <set>

using namespace std;
char field[1001][1001];
bool used[1001][1001];

int main(){
  int H,W;
  while(cin>>H>>W&&(H|W)){
    for(int i=0;i<H;i++)
      for(int j=0;j<W;j++)
	cin>>field[i][j];
    vector<string> vs;
    for(int dy=-1;dy<=1;dy++){
      for(int dx=-1;dx<=1;dx++){
	if(dy==0&&dx==0)continue;
	memset(used,0,sizeof(used));
	for(int i=0;i<H;i++){
	  for(int j=0;j<W;j++){
	    if(!(i==0||j==0||i==H-1||j==W-1))continue;
	    int cx=j;
	    int cy=i;
	    string s;
	    if(used[cy][cx])continue;
	    while(1){
	      used[cy][cx]=true;
	      s+=field[cy][cx];
	      cy+=dy;cx+=dx;
	      cy=(cy+H)%H;
	      cx=(cx+W)%W;
	      if(used[cy][cx])break;
	    }
	    vs.push_back(s);
	    //cout<<s<<endl;
	  }
	}
      }
    }
    //unordered_set<string> us;
    set<string> us;
    string cand;
    for(int i=0;i<(int)vs.size();i++){
      for(int j=0;j<(int)vs[i].size();j++){
	for(int k=1;k<=(int)vs[i].size();k++){
	  string s;
	  for(int l=0;l<k;l++){
	    s+=vs[i][(j+l)%vs[i].size()];
	  }
	  if(us.count(s)==1){
	    if(cand==""||cand.size()<s.size())
	      cand=s;
	    else if(cand.size()==s.size()&&cand>s)
	      cand=s;
	  }
	  else if(us.count(s)==0)
	    us.insert(s);
	}
      }
    }
    if(cand.size()>1)cout<<cand<<endl;
    else cout<<0<<endl;
  }

  return 0;
}