Go: package main

import "fmt"

var H, W int

type point struct {
	x, y int
}

func (p *point) add(q point) {
	p.x += q.x
	p.y += q.y
	if p.x < 0 {
		p.x += H
	}
	if p.y < 0 {
		p.y += W
	}
	p.x %= H
	p.y %= W
}

func isPalindrome(str string) bool {
	for i, j := 0, len(str)-1; i < j; i, j = i+1, j-1 {
		if str[i] != str[j] {
			return false
		}
	}
	return true
}

func magicSpel(arr []string) string {
	var ans string
	m := map[string]int{}
	for i := range arr {
		for j := range arr[i] {
			for _, d := range []point{{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}, {0, 1}, {1, -1}, {1, 0}, {1, 1}} {
				cur := point{x: i, y: j}
				str := string(arr[cur.x][cur.y])
				cur.add(d)
				for i != cur.x || j != cur.y {
					str += string(arr[cur.x][cur.y])
					cur.add(d)
					if isPalindrome(str) {
						m[str] += 2
					} else {
						m[str]++
					}
				}
			}
		}
	}
	for s, v := range m {
		if 1 < v {
			if len(ans) < len(s) {
				ans = s
			} else if len(ans) == len(s) && s < ans {
				ans = s
			}
		}
	}

	if len(ans) == 0 {
		return "0"
	}

	return ans
}

func main() {
	for {
		fmt.Scan(&H, &W)
		if H == 0 && W == 0 {
			break
		}

		var arr []string

		for i := 0; i < H; i++ {
			var s string
			fmt.Scan(&s)
			arr = append(arr, s)
		}

		fmt.Println(magicSpel(arr))
	}
}

// 2 3
// ABC
// DEF

C++: #include <bits/stdc++.h>
using namespace std;

int dy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
int h, w;

vector<string> lt;
map<string, int> mp;

void dfs(int sy, int sx, int y, int x, int dir, string s) {
    int ny = (y + dy[dir] + h) % h;
    int nx = (x + dx[dir] + w) % w;
    if (sy != ny || sx != nx) {
        string ns = s + lt[ny][nx];
        mp[ns] ++;
        dfs(sy, sx, ny, nx, dir, ns);
    }
}

int main() {
    while (cin >> h >> w) {
        if (!h) break;
        lt.resize(h);
        for (int i=0; i<h; ++i) {
            cin >> lt[i];
        }
        mp.clear();
        for (int i=0; i<h; ++i) {
            for (int j=0; j<w; ++j) {
                for (int k=0; k<8; ++k) {
                    string s(1, lt[i][j]);
                    dfs(i, j, i, j, k, s);
                }
            }
        }
        string magic = "";
        int len = -1;
        for (auto it=mp.begin(); it!=mp.end(); ++it) {
            if (it->second < 2) continue;
            if (len < (int)(it->first).size() || (len == (int)(it->first).size() && it->first < magic)) {
                len = (it->first).size();
                magic = it->first;
            }
        }
        if (magic == "") {
            cout << 0 << endl;
        } else {
            cout << magic << endl;
        }
    }
    return 0;
}

C++: #include <map>
#include <vector>
#include <iostream>
using namespace std;
int m[29][29];
main(){
	string s,t;
	int h,w,i,j,x,y;
	for(;cin>>h>>w,h;cout<<s<<endl){
		vector<string>v;
		for(i=0;i<h;i++)cin>>s,v.push_back(s);
		s="0";
		map<string,int>M;
		for(i=0;i<h;i++)for(j=0;j<w;j++)for(x=-1;x<=1;x++)for(y=-1;y<=1;y++){
			if(x==0&&y==0)continue;
			memset(m,0,sizeof(m));
			int Y=i,X=j;
			t=v[Y][X],m[Y][X]=1;X=(X+x+w)%w,Y=(Y+y+h)%h;
			for(;!m[Y][X];X=(X+x+w)%w,Y=(Y+y+h)%h){
				t+=v[Y][X],m[Y][X]=1;
				if(++M[t]>1 && (s.size()<t.size()||(s.size()==t.size()&&s>t)))s=t;
			}
		}
	}
}

C++: #include <iostream>
#include <iomanip>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <functional>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef istringstream ISS;
typedef ostringstream OSS;

#define REP( i, m, n ) for ( int i = (int)( m ); i < (int)( n ); ++i )
#define FOR( v, c ) for ( auto &v : c )
#define EACH( it, c ) for ( auto it = c.begin(); it != c.end(); ++it )
#define ALL( c ) (c).begin(), (c).end()
#define DRANGE( c, p ) (c).begin(), (c).begin() + p, (c).end()

#define PB( n ) push_back( n )
#define MP( a, b ) make_pair( ( a ), ( b ) )
#define EXIST( c, e ) ( (c).find( e ) != (c).end() )

#define fst first
#define snd second

#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define DEBUG( x ) cerr << __FILE__ << ":" << __LINE__ << ": " << #x << " = " << ( x ) << endl

int main()
{
	cin.tie( 0 );
	ios::sync_with_stdio( false );

	for ( int h, w; cin >> h >> w, h | w; )
	{
		VS board( h );
		FOR( line, board )
		{
			cin >> line;
		}

		map<string,int> counts;
		REP( i, 0, h )
		{
			REP( j, 0, w )
			{
				REP( dy, -1, 2 )
				{
					REP( dx, -1, 2 )
					{
						int y = i, x = j;
						string cur;
						VVI visited( h, VI( w ) );

						do
						{
							cur += board[y][x];
							counts[ cur ] ++;

							visited[y][x] = 1;
							( y += dy + h ) %= h;
							( x += dx + w ) %= w;
						}
						while ( !visited[y][x] );
					}
				}
			}
		}

		string res;
		FOR( it, counts )
		{
			if ( it.snd < 2 )
			{
				continue;
			}

			if ( res.size() < it.fst.size() )
			{
				res = it.fst;
			}
			else if ( res.size() == it.fst.size() )
			{
				res = min( res, it.fst );
			}
		}

		cout << ( 2 <= res.size() ? res : "0" ) << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
using namespace std;

const int MAX_H = 11;
const int MAX_W = 21;

map<string, int> M;
vector<string> V;
int H, W;
char G[MAX_H][MAX_W];

bool compare(const string &a, const string &b) {
  if(a.size() != b.size()) return a.size() > b.size();
  return a < b;
}

string solve() {
  V.clear();
  M.clear();
  for(int i = 0; i < H; ++i) {
    for(int j = 0; j < W; ++j) {
      for(int dx = -1; dx <= 1; ++dx) {
	for(int dy = -1; dy <= 1; ++dy) {
	  if(dx == 0 && dy == 0) continue;
	  int x = j;
	  int y = i;
	  string s = "";
	  do {
	    s += G[y][x];
	    if(s.size() >= 2 && ++M[s] == 2) {
	      V.push_back(s);
	    }
	    x = (x+dx+W)%W;
	    y = (y+dy+H)%H;
	  } while(!(x == j && y == i));
	}
      }
    }
  }
  if(V.size() == 0) return "0";
  sort(V.begin(), V.end(), compare);
  return V[0];
}

int main() {
  while(cin >> H >> W && (H | W)) {
    for(int i = 0; i < H; ++i) {
      for(int j = 0; j < W; ++j) {
	cin >> G[i][j];
      }
    }
    cout << solve() << endl;
  }
  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
#define ll long long
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1316

int h, w;

int dy[8] = { -1, -1, -1, 0, 0, 1, 1, 1 };
int dx[8] = { -1, 0, 1, -1, 1, -1, 0, 1 };

set<string> Set;
int Maxlen = 0;
string ans;
vector<vector<char>> donut;
vector<vector<int>> usage;

void solve(int i, int j) {
	for (int k = 0; k < 8;k++) {
		usage.resize(h, vector<int>(w, 0));
		string ret = "";
		int y = i, x = j;
		while (usage[y][x] == 0) {
			ret += donut[y][x];

			if (Set.find(ret) != Set.end()) {
				if (ret.length() > Maxlen) {
					Maxlen = ret.length();
					ans = ret;
				}
				else if (ret.length() == Maxlen && ret < ans) {
					ans = ret;
				}
			}
			else {
				Set.insert(ret);
			}

			usage[y][x] = 1;
			y = (y + dy[k] + h) % h;
			x = (x + dx[k] + w) % w;


		}
	}
}

int main() {
	cin.tie(0); ios::sync_with_stdio(false);
	while (cin >> h >> w) {
		if (h == 0 && w == 0)break;
		Maxlen = 0;
		ans = "";
		donut.resize(h, vector<char>(w));
		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				cin >> donut[i][j];
			}
		}

		for (int i = 0; i < h;i++) {
			for (int j = 0; j < w;j++) {
				solve(i, j);
			}
		}
		cout << ans << endl;
	}

}