#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <time.h>
#include <queue>
#include <stack>
#include <cctype>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <sstream>
#define Mod 1000000007
#define SMod 10007
#define INint 2147483647
#define pi acos(-1.0)
#define eps 1e-4
#define lll __int64
#define ll long long
using namespace std;
#define N 12
#define M 22

char ss[N][M];
int dx[8] = {-1,-1,-1,0,0,1,1,1};
int dy[8] = {-1,0,1,1,-1,-1,0,1};
string S1,S2,ans;
int n,m;

void go1(int x,int y,int prex,int prey,int k)
{
    if(x == prex && y == prey)
        return;
    S1 += ss[x][y];
    int nx = x+dx[k];
    int ny = y+dy[k];
    nx = (nx+n)%n;
    ny = (ny+m)%m;
    go1(nx,ny,prex,prey,k);
    return;
}

void go2(int x,int y,int prex,int prey,int k)
{
    if(x == prex && y == prey)
        return;
    S2 += ss[x][y];
    int nx = x+dx[k];
    int ny = y+dy[k];
    nx = (nx+n)%n;
    ny = (ny+m)%m;
    go2(nx,ny,prex,prey,k);
    return;
}

int main()
{
    int i,j,k,h,ka,kb,u,v;
    while(scanf("%d%d",&n,&m)!=EOF && n+m)
    {
        memset(ss,0,sizeof(ss));
        for(i=0;i<n;i++)
            scanf("%s",ss[i]);
        int tag = 0;
        int maxi = 0;
        ans = "";
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                for(k=0;k<n;k++)
                {
                    for(h=0;h<m;h++)
                    {
                        if(ss[i][j] == ss[k][h])
                        {
                            for(ka=0;ka<8;ka++)
                            {
                                for(kb=0;kb<8;kb++)
                                {
                                    if(i == k && j == h && ka == kb)
                                        continue;
                                    S1 = "";
                                    S1 += ss[i][j];
                                    int nx = i+dx[ka];
                                    int ny = j+dy[ka];
                                    nx = (nx+n)%n;
                                    ny = (ny+m)%m;
                                    go1(nx,ny,i,j,ka);
                                    //cout<<S1<<endl;
                                    S2 = "";
                                    S2 += ss[k][h];
                                    int kx = k+dx[kb];
                                    int ky = h+dy[kb];
                                    kx = (kx+n)%n;
                                    ky = (ky+m)%m;
                                    go2(kx,ky,k,h,kb);
                                    //cout<<S2<<endl;
                                    int cnt = 0;
                                    int len1 = S1.length();
                                    int len2 = S2.length();
                                    for(u=0;u<min(len1,len2);u++)
                                    {
                                        if(S1[u] != S2[u])
                                            break;
                                    }
                                    cnt = u;
                                    if(cnt > maxi)
                                    {
                                        ans = S1.substr(0,u);
                                        //cout<<"Sub: "<<S1.substr(0,u)<<endl;
                                        maxi = cnt;
                                    }
                                    else if(cnt == maxi)
                                    {
                                        if(ans > S1.substr(0,u))
                                            ans = S1.substr(0,u);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if(ans.length() <= 1)
            puts("0");
        else
            cout<<ans<<endl;
    }
    return 0;
}

Measured memory_bytes: 1236