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 <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#include <map>

using namespace std;
#define loop(i,a,b) for(int i=(a);i<int(b);i++)
#define rep(i,b) loop(i,0,b)

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

string g[100];

int h,w;
int main(){
    while(cin>>h>>w && h){
        map<string,int> m;
        rep(i,h)cin>>g[i];
        rep(i,h)rep(j,w){
            rep(d,4){
                string s;
                for(int ii=i, jj=j; s.size()==0 || ii!=i || jj!=j;
                    ii=(ii+h+dy[d])%h, jj=(jj+w+dx[d])%w){
                    s += g[ii][jj];
                    m[s]++;
                    reverse(s.begin(),s.end());
                    m[s]++;
                    reverse(s.begin(),s.end());
                }
            }
        }
        string ans;
        for(auto& p:m){
            //cout << p.first << " " << p.second << endl;
            if(p.second < 2) continue;
            if(ans.size() > p.first.size()) continue;
            if(ans.size() == p.first.size()){
                ans = min(ans,p.first);
            }
            if(ans.size() < p.first.size()) ans = p.first;
        }
        if(ans.size() <= 1) ans="0";
        cout<<ans<<endl;
        getchar();
    }
}

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

int main(){
  int h,w;
  int dx[] = {-1,0,1,-1,1,-1,0,1};
  int dy[] = {1,1,1,0,0,-1,-1,-1};
  while(cin >> h >> w && h && w){
    char c[11][21];
    set<string> s;
    string ans = "";

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	cin >> c[i][j];
      }
    }

    for(int i=0;i<h;i++){
      for(int j=0;j<w;j++){
	for(int k=0;k<8;k++){
	  int x = j;
	  int y = i;

	  string str;
	  str += c[y][x];

	  while(1){
	    x += dx[k];
	    y += dy[k];

	    if(x < 0) x = w - 1;
	    else if(x >= w) x = 0;
	    if(y < 0) y = h - 1;
	    else if(y >= h) y = 0;

	    if(x == j && y == i) break;
	    else str += c[y][x];

	    
	    if(!s.empty() && s.find(str) != s.end() && str.size() > ans.size()) ans = str;
	    else if(!s.empty() && s.find(str) != s.end() && str.size() == ans.size() && str < ans) ans = str;
	    else s.insert(str);
	    
	  }

	}
      }
    }
    if(ans != "") cout << ans << endl;
    else cout << "0" << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>

#include <tr1/unordered_map>

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)


using namespace std;

char d[20][30];

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

int main(){
  int h, w;

  while(scanf("%d%d", &h, &w), h + w){
    tr1::unordered_map<string, int> m;

    REP(i,h) scanf("%s", d[i]);

    REP(i,h) REP(j,w){
      REP(k,8){
        int x = j;
        int y = i;
        string tmp = "";

        do{
          tmp += d[y][x];
          x = (x + w + dx[k]) % w;
          y = (y + h + dy[k]) % h;
          m[tmp]++;
        }while(i != y || j != x);
      }
    }

    string ans = "";

    FOR(it, m) if(it->second > 1){
      const string &s = it->first;

      if(s.size() > ans.size()) ans = s;
      else if(s.size() == ans.size() && s < ans) ans = s;
    }

    if(ans.size() > 1) puts(ans.c_str());
    else puts("0");
  }

  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 310;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, 1, -1};

int ch[400000][26];
int sz;
char mz[22][22];
char str[N], ans[N];
int la;
int n, m;

void getstr(int sx, int sy, int d, char *s) {
    int len = 0;
    int x = sx, y = sy;
    do {
        s[len++] = mz[x][y];
        x += dx[d]; x = (x + n) % n;
        y += dy[d]; y = (y + m) % m;
    } while (x != sx || y != sy);
    s[len] = 0;
}

char t[N];
int lt;
void insert(char *s) {
//puts(s);
    int u = 0;
    t[lt = 0] = 0;
    bool ok = true;
    for (int i = 0; s[i]; i++) {
        int d = s[i] - 'A';
        if (ch[u][d] == 0) {
            memset(ch[sz], 0, sizeof(ch[sz]));
            ch[u][d] = sz++;
            ok = false;
        }
        u = ch[u][d];
        if (ok) {
            t[lt++] = s[i]; t[lt] = 0;
        }
    }
}

void work() {
    memset(ch[0], 0, sizeof(ch[0]));
    sz = 1;
    for (int i = 0; i < n; i++) scanf("%s", mz[i]);
    ans[la = 0] = 0;
    for (int i = 0; i < n; i++)
        for (int j = 0; j < m; j++) {
            for (int d = 0; d < 8; d++) {
                getstr(i, j, d, str);
                insert(str);
                if (lt > la) {
                    la = lt;
                    strcpy(ans, t);
                } else if (lt == la && strcmp(t, ans) < 0) {
                    strcpy(ans, t);
                }
            }
        }
    if (la <= 1) puts("0");
    else puts(ans);
}

int main() {
//freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
    while (scanf("%d%d", &n, &m), n || m) {
        work();
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>

using namespace std;

#define int long long
#define endl "\n"

const long long INF = (long long)1e18;
const long long MOD = (long long)1e9 + 7; 

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define MAX

vector<string> str;
map<string, int> mp;
int h, w, maximum, co;
string ans;


void check(int y, int x){
	vector<vector<int>> used(h,vector<int>(w,0));
	int count = 1;
	string res;
	// cout<<"y = "<<y<<" x = "<<x<<endl;
	for(int i = -1; i <= 1; i++){
		for(int j = -1; j <= 1; j++){
			string temp;
			// cout<<"i = "<<i<<" j = "<<j<<endl;
			for(int ny = y, nx = x; count != used[ny][nx]; nx = (j+w+nx)%w, ny = (i+h+ny)%h){
				used[ny][nx] = count;
				temp.push_back(str[ny][nx]);
				// cout<<temp<<endl;
				if(temp.size() <= 1) continue;
				
				if(++mp[temp] != 2) continue;
				if(ans.size() < temp.size()){
					ans = temp;
				} else if(ans.size() == temp.size() && ans > temp){
					ans = temp;
				}
			}
			
			count++;
		}
	}
	
	
	// return res;
}

signed main(){
	// cin.tie(0);
	// ios::sync_with_stdio(false);
	// cout<<fixed<<setprecision(10);
	
	while(true){
		mp.clear();
		ans.clear();
		
		cin>>h>>w;
		
		if(!h && !w) break;
		
		str.clear();
		str.resize(h);
		
		for(int i = 0; i < h; i++){
			cin>>str[i];
		}
		
		for(int i = 0; i < h; i++){
			for(int j = 0; j < w; j++){
				check(i,j);
			}
		}
		
		cout<<(ans.size()?ans:"0")<<endl;
	}
	
	
	return 0;
}