#include<iostream>
#include<vector>
#include<algorithm>
#include<cstring>
 
using namespace std;
 
const int MAXH = 10;
const int MAXW = 20;
int W,H;
char field[MAXH][MAXW];
bool come[MAXH][MAXW];
int dx[] = {0,1,0,-1,1,1,-1,-1};
int dy[] = {1,0,-1,0,1,-1,1,-1};
 
void input(){
  for(int i = 0; i < H; i++)
    for(int j = 0; j < W; j++) cin >> field[i][j];
}
 
string comp(const string& s1, const string& s2){
   
  int len = min(s1.length(),s2.length());
  string s ="";
 
  for(int i = 0; i < len; i++){
    if(s1[i] != s2[i]) return s;
    s+=s1[i];
  }
  return s;
}
 
void print(const vector<string>& v){
  cout << endl;
  for(int i = 0; i < v.size(); i++)
    cout << v[i] << endl;
  cout << endl;
}
 
void solve(){
  vector<string> V;
 
  for(int i = 0; i < H; i++) 
    for(int j = 0; j < W; j++)
      for(int d = 0; d < 8; d++){
        memset(come,false,sizeof(come));
 
        string s = "";
        int x=j,y=i;
 
        while(!come[y][x]){
          s += field[y][x];
          come[y][x] = true;
          y = (y+dy[d]+H)%H;
          x = (x+dx[d]+W)%W;
        }
 
        V.push_back(s);
      }
 
  sort(V.begin(),V.end());
  // print(V);
  
  string ans = "";
 
  for(int i = 1; i < V.size(); i++){
     
    string tmp = comp(V[i-1],V[i]);
     
    if(tmp.length() == ans.length()) ans = min(ans,tmp);
    else if(tmp.length() > ans.length()) ans = tmp;
  }
   
  if(ans.length() > 1)cout << ans << endl;
  else cout << "0" << endl;
}
 
int main(){
 
  while(cin >> H >> W && H+W){
    input();
    solve();
  }
  return 0;
}