1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
| #include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <map>
#include <set>
#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30
#define pb push_back
#define mp make_pair
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int w,h,x,y;
int sx,sy,gx,gy;
int dx[9] = { 1, 1, 1, 0, 0, 0,-1,-1,-1};
int dy[9] = {-1, 0, 1,-1, 0, 1,-1, 0, 1};
bool can(int y,int x) {
if(0 <= y && y < h && 0 <= x && x < w) return true;
return false;
}
int main() {
while(cin >> h >> w) {
if(h == 0 && w == 0) break;
vector<string> s(h);
rep(i, h) cin >> s[i];
map<string, int> m;
bool used[15][25];
rep(i, h) {
rep(j, w) {
rep(k, 9) {
int y = i, x = j;
memset(used, 0, sizeof(used));
string t = "";
while(!used[y][x]) {
used[y][x] = true;
t += s[y][x];
y = (y + h + dy[k]) % h;
x = (x + w + dx[k]) % w;
if(t.size() > 1) m[t]++;
}
}
}
}
string ans = "";
map<string, int>::iterator ite;
for(ite = m.begin(); ite != m.end(); ite++) {
if(ite->second < 2) continue;
if(ite->first.size() > ans.size()) {
ans = ite->first;
} else if(ite->first.size() == ans.size()) {
bool flag = true;
rep(i, ans.size()) {
if(ite->first[i] <= ans[i]) continue;
flag = false;
}
if(flag) ans = ite->first;
}
}
if(ans.size() == 0) cout << 0 << endl;
else cout << ans << endl;
}
return 0;
}
|