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 <cstdio>
#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

constexpr long long MAX = 5100000;
constexpr long long INF = 1LL << 60;
constexpr int inf = 1 << 28;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353LL;

using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int dh[8] = { 0,1,1,1,0,-1,-1,-1 };
int dw[8] = { 1,1,0,-1,-1,-1,0,1 };
vector<string> vs;
map<string, ll> mp;
void solve() {
	int H, W; cin >> H >> W; if (H == 0 and W == 0) exit(0);
	vs.resize(H); for (int i = 0; i < H; i++) cin >> vs[i];
	mp.clear();
	for (int h = 0; h < H; h++) {
		for (int w = 0; w < W; w++) {
			for (int i = 0; i < 8; i++) {
				int nh = h;
				int nw = w;
				string s;
				s += vs[nh][nw];
				while (1) {
					nh += dh[i];
					nw += dw[i];
					if (nh >= H) nh = 0;
					if (nw >= W) nw = 0;
					if (nh < 0) nh = H - 1;
					if (nw < 0) nw = W - 1;
					if (nh == h and nw == w) break;
					s += vs[nh][nw];
					mp[s]++;
				}
			}
		}
	}
	string res;
	for (auto& p : mp) {
		if (p.second >= 2) {
			if (res.size() < p.first.size()) res = p.first;
			else if (res.size() == p.first.size() and res > p.first) res = p.first;
		}
	}
	if (res == "") cout << "0" << "\n";
	else cout << res << "\n";
}

int main()
{
	
	/*
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	*/

	int kkt = 89;
	while (kkt) {
		solve();
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using ll = long long;
const char newl = '\n';
#define var auto

void solver(int h, int w) {
  vector< vector< char > > cs(h, vector<char>(w));
  for (int i = 0; i < h; i += 1) {
    for (int j = 0; j < w; j += 1) {
      cin >> cs[i][j];
    }
  }

  vector< int > ds{-1, 0, +1};
  string ans;
  set< string > st;

  for (int i = 0; i < h; i += 1) {
    for (int j = 0; j < w; j += 1) {
      for (auto dy: ds) for (auto dx: ds) {
        if (dy == 0 && dx == 0) continue;

        string s;
        int y = i, x = j;

        do {
          s += cs[y][x];
          (y += dy + h) %= h;
          (x += dx + w) %= w;
          
          if (st.count(s)) {
            if (ans.size() < s.size()) ans = s;
            else if (ans.size() == s.size() && ans > s) ans = s;
          }
            
          st.insert(s);
        } while (y != i || x != j); 
      }
    }
  }

  if (ans.size() >= 2) cout << ans << endl;
  else cout << 0 << endl;
}

signed main(){
    int h, w;
    while (cin >> h >> w, h) {
        solver(h, w);
    }
}

C++: #include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#include <cfloat>

using std::cerr;
using std::cin;
using std::cout;
using std::endl;

int main(void){
  /*cout << std::fixed << std::setprecision(16);
  cin.tie(0);
  std::ios::sync_with_stdio(false);*/

  char S[99][99];

  for(;;){
    int h,w;
    scanf("%d %d",&h,&w);
    if(h==0 && w==0) break;

    for(int i = 0; i < h; ++i)
      scanf("%s",S[i]);

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

    std::vector<std::string> spells;

    for(int i = 10000*w; i < 10001*w; ++i){
      for(int j = 10000*h; j < 10001*h; ++j){
        for(int k = 0; k < dn; ++k){
          std::string sub;
          int u = i,v = j;
          do{
            sub += S[v%h][u%w];
            v += dx[k];
            u += dy[k];
          }while(u % w != i % w || v % h != j % h);
          spells.push_back(sub);
        }
      }
    }

    /*bool f = true;
    std::string ans;
    int anslength = 0;
    for(auto c:Counts){
      if(c.second == 2){
        if(c.first.size() > anslength){
          anslength = c.first.size();
          ans = c.first;
        }
        f = false;
      }
    }
    if(f) printf("0\n");
    else printf("%s\n",ans.c_str());
     */
    sort(spells.begin(), spells.end());

    int ansl = -1;
    std::string ans;
    for(int i = 0 ; i+1 < spells.size();++i){
      int c = 0;
      for(int j = 0; spells[i][j] == spells[i+1][j]&& spells[i][j]; ++j, c = j);
      if(c > ansl){
        ansl = c;
        ans  = spells[i].substr(0, c);
      }
    }

    if(ans.size() > 1)
    printf("%s\n", ans.c_str());
else printf("0\n");

  }


  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long   // <-----!!!!!!!!!!!!!!!!!!!

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n)-1;i>=0;i--)
#define rrep2(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define all(a) (a).begin(),(a).end()

typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> TUPLE;
typedef vector<int> V;
typedef vector<V> VV;
typedef vector<VV> VVV;

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

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

    int H, W;
    while (cin >> H >> W, H) {
        vector<string> s(H);
        rep(i, H) cin >> s[i];

        map<string, int> mp;
        rep(i, H) {
            rep(j, W) {
                rep(k, 8) {
                    string t = "";
                    set<P> st;
                    int x = i;
                    int y = j;
                    while (!st.count(P(x, y))) {
                        t += s[x][y];
                        mp[t]++;
                        st.insert(P(x, y));
                        x = (x + dx[k] + H) % H;
                        y = (y + dy[k] + W) % W;
                    }
                }
            }
        }

        string ans = "0";
        for (auto p : mp) {
            if (p.first.size() > ans.size() && p.second >= 2) {
                ans = p.first;
            }
        }

        cout << ans << endl;
    }
}