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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
| #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 h, w;
bool used[1005][1005];
vector<string> s, v(2);
int func(int i, int j) {
int ret = 0;
if(i-1 >= 0 && j-1 >= 0) {
if(s[i-1][j-1] == v[0][0] && s[i-1][j] == v[0][1] && s[i][j-1] == v[1][0] && s[i][j] == v[1][1]) {
ret++;
}
}
if(i-1 >= 0 && j+1 < w) {
if(s[i-1][j] == v[0][0] && s[i-1][j+1] == v[0][1] && s[i][j] == v[1][0] && s[i][j+1] == v[1][1]) {
ret++;
}
}
if(i+1 < h && j-1 >= 0) {
if(s[i][j-1] == v[0][0] && s[i][j] == v[0][1] && s[i+1][j-1] == v[1][0] && s[i+1][j] == v[1][1]) {
ret++;
}
}
if(i+1 < h && j+1 < w) {
if(s[i][j] == v[0][0] && s[i][j+1] == v[0][1] && s[i+1][j] == v[1][0] && s[i+1][j+1] == v[1][1]) {
ret++;
}
}
return ret;
}
int main() {
cin >> h >> w;
s.resize(h);
rep(i, h) cin >> s[i];
rep(i, 2) cin >> v[i];
int ans = 0;
memset(used, 0, sizeof(used));
rep(i, h-1) {
rep(j, w-1) {
if(s[i][j] == v[0][0] && s[i][j+1] == v[0][1] && s[i+1][j] == v[1][0] && s[i+1][j+1] == v[1][1]) {
ans++;
used[i][j] = true;
used[i][j+1] = true;
used[i+1][j] = true;
used[i+1][j+1] = true;
}
}
}
int res = 0;
rep(i, h) {
rep(j, w) {
int t = func(i, j), t2 = 0;
char c = s[i][j];
s[i][j] = 'J';
t2 = func(i, j);
res = max(res, t2 - t);
s[i][j] = 'O';
t2 = func(i, j);
res = max(res, t2 - t);
s[i][j] = 'I';
t2 = func(i, j);
res = max(res, t2 - t);
s[i][j] = c;
}
}
cout << ans + res << endl;
return 0;
}
|