#include <bits/stdc++.h>
// --------------------
#define sharky using namespace
#define fai std
#define wrong ios_base::sync_with_stdio(0);
#define answer cin.tie(0);
// --------------------
sharky fai;
#define ll long long
#define ld long double
#define all(x) x.begin(), x.end()
#define sz(x) (ll) (x).size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define endl "\n"
#define vll vector<ll>
#define pl pair<ll, ll>
const int inf = 1e9 + 5;
const int MOD = 1e9 + 7;
#define FOR(i, a, b) for (ll i = a; i < (b); i++)
#define F0R(i, a) for (ll i = 0; i < (a); i++)
bool isWhite(ll r, ll g, ll b) {
ll tmp = max(abs(r - g), abs(g - b));
tmp = max(tmp, abs(r - b));
ll tmp2 = min(r, g); tmp2 = min(tmp2, b);
return (tmp <= 15) & (tmp2 >= 170);
}
bool isGreen(ll r, ll g, ll b) {
return min(g, r) >= b && !isWhite(r, g, b);
}
bool isBlack(ll r, ll g, ll b) {
return r <= 15 && g <= 15 && b <= 15;
}
int main() {
//g++ -Wl,-stack_size,0x20000000 I1312.cpp -o 1312
ios_base::sync_with_stdio(0); cin.tie(0);
ll h, w; cin >> h >> w; ll a[h + 5][w + 5][3]; ll sim = 0;
ll gcnt = 0, wcnt = 0;
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++) {
cin >> a[i][j][0] >> a[i][j][1] >> a[i][j][2];
if (isGreen(a[i][j][0], a[i][j][1], a[i][j][2])) gcnt++;
else if (isWhite(a[i][j][0], a[i][j][1], a[i][j][2])) wcnt++;
}
}
for (ll i = 0; i < h - 1; i++) {
for (ll j = 0; j < w - 1; j++) {
if (abs(a[i][j][0] - a[i][j + 1][0]) <= 20 && abs(a[i][j][1] - a[i][j + 1][1]) <= 20 && abs(a[i][j][2] - a[i][j + 1][2]) <= 20) {
sim++;
}
if (abs(a[i][j][0] - a[i + 1][j][0]) <= 20 && abs(a[i][j][1] - a[i + 1][j][1]) <= 20 && abs(a[i][j][2] - a[i + 1][j][2]) <= 20) {
sim++;
}
}
}
ll black = 0;
for (ll i = 0; i < h; i++) {
for (ll j = 0; j < w; j++) {
if (isBlack(a[i][j][0], a[i][j][1], a[i][j][2])) black++;
}
}
// cout << endl;
// cout << sim << " " << gcnt << " " << black << endl;
if (sim <= ((h - 1) * (w - 1))) {
cout << "3\n"; return 0;
}
else if (gcnt >= (h * w) / 2) {
cout << "2\n"; return 0;
} else if (sim >= ((h - 1) * (w - 1) * 1.25)){
if ((black <= (h - 1) * (w - 1) / 2 && black >= 100)) {
cout << "1\n";
} else {
cout << "4\n";
}
return 0;
}
cout << "3\n";
return 0;
}
/* 689553
1 2 3 4 8 9 10 15 16 19 22 27 28 35 36 39 40 41 44 46 48 49 59 62 65 68 72 74 75 77 78 79 81 84 85 86 87 89 92 97 98
*/
Compilation message
/usr/bin/ld: /tmp/cc6EkvOM.o: in function `main':
artclass.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/ccWBmHxP.o:grader.c:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/ccWBmHxP.o: in function `main':
grader.c:(.text.startup+0x200): undefined reference to `style'
collect2: error: ld returned 1 exit status