#include <iostream>
#include <map>
#include <queue>
#include <algorithm>
#include <vector>
#include <string>
#include <memory.h>
using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
typedef pair<int, int> P;
typedef pair<int, P> state;
const int INF = 1 << 28;

map<string, int> cnt;
bool used[20][20];
string s[20];
int h, w;
int dx[] = {1, 1, 1, 0, 0, -1, -1, -1};
int dy[] = {0, -1, 1, -1, 1, 0, -1, 1};

void make_string(int y, int x){
  
  rep(k, 8){
    int i = y;
    int j = x;
    string str;
    memset(used, 0, sizeof(used));
    while(!used[i][j]){
      str += s[i][j];
      cnt[str]++;
      used[i][j] = true;
      i += dy[k];
      j += dx[k];
      i = (i + h) % h;
      j = (j + w) % w;
    }
  }
}

int main(){
  while(cin >> h >> w && (h || w)){
    string res = "";
    cnt.clear();
    rep(i, h) cin >> s[i];
    rep(i, h)rep(j, w){
      make_string(i, j);
    }
    
    for(map<string, int>::iterator iter = cnt.begin(); iter != cnt.end(); iter++){
      if(iter->second > 1 && (iter->first).size() > res.size()) res = iter->first;      
    }
  
    if((int)res.size() < 2) res = "0";
    cout << res << endl;
  }
  return 0;
}