#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <algorithm>

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main() {
  while(1) {
    int h,w;
    cin>>h>>w;
    if(!h)break;
    vector<string> t(h);
    REP(i,h)cin>>t[i];
    vector<string> alls;
    REP(i,h)REP(j,w) {
      int di[]={1,1,1,0,-1,-1,-1,0};
      int dj[]={1,0,-1,-1,-1,0,1,1};
      REP(k,8) {
        string str;
        REP(l,h*w) {
          int ni=(i+di[k]*l+h*w)%h;
          int nj=(j+dj[k]*l+h*w)%w;
          if(l&&ni==i&&nj==j) break;
          str += t[ni][nj];
          alls.push_back(str);
        }
      }
    }
    sort(begin(alls),end(alls),[](const string& lhs, const string& rhs){
      if (lhs.size() > rhs.size()) return true;
      if (lhs.size() == rhs.size() && lhs < rhs) return true;
      return false;
    });
    int n=alls.size();
    string ans="0";
    REP(i,n-1) {
      if (alls[i].size() == 1) break;
      if(alls[i] == alls[i+1]) {
        ans = alls[i];
        break;
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}