#include <bits/stdc++.h>
using namespace std;
class IMAGE {
public:
struct Pixel {
int r, g, b;
Pixel(int x = 0, int y = 0, int z = 0):
r(x), g(y), b(z) {}
void read() {
scanf("%d %d %d", &r, &g, &b);
}
Pixel operator-(const Pixel &x) const {
return Pixel(r-x.r, g-x.g, b-x.b);
}
Pixel operator+(const Pixel &x) const {
return Pixel(r+x.r, g+x.g, b+x.b);
}
Pixel operator*(const int x) const {
return Pixel(r*x, g*x, b*x);
}
Pixel operator/(const int x) const {
return Pixel(r/x, g/x, b/x);
}
void print() {
printf("%d %d %d", r, g, b);
}
int length() {
return abs(r) + abs(g) + abs(b);
}
};
int W, H;
static const int MAXN = 256;
Pixel data[MAXN][MAXN];
int energy[MAXN][MAXN], dp[MAXN][MAXN];
void read() {
scanf("%d %d", &W, &H);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j].read();
}
Pixel pabs(Pixel x) {
return Pixel(fabs(x.r), fabs(x.g), fabs(x.b));
}
inline Pixel getPixel(int x, int y) {
if (x >= 0 && y >= 0 && x < H && y < W)
return data[x][y];
if (y < 0) return data[min(max(x, 0), H-1)][0];
if (y >= W) return data[min(max(x, 0), H-1)][W-1];
if (x < 0) return data[0][min(max(y, 0), W-1)];
if (x >= H) return data[H-1][min(max(y, 0), W-1)];
return Pixel(0, 0, 0);
}
int sobel(int i, int j) {
const static int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};
const static int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
const static int xw[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
const static int yw[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
Pixel Dx(0, 0, 0), Dy(0, 0, 0);
for (int k = 0; k < 9; k++) {
if (xw[k])
Dx = Dx + getPixel(i+dx[k], j+dy[k]) * xw[k];
if (yw[k])
Dy = Dy + getPixel(i+dx[k], j+dy[k]) * yw[k];
}
return Dx.length() + Dy.length();
}
void shrink(int n) {
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
energy[i][j] = sobel(i, j);
int path[MAXN];
for (int it = 0; it < n; it++) {
shrink(path);
for (int i = 0; i < H; i++) {
int y = path[i];
for (int j = y - 3; j < W; j++) {
if (j >= 0)
energy[i][j] = sobel(i, j);
}
}
}
}
void shrink(int path[]) {
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
int &val = dp[i][j];
if (i == 0) val = energy[i][j];
else {
val = dp[i-1][j];
if (j-1 >= 0)
val = min(val, dp[i-1][j-1]);
if (j+1 < W)
val = min(val, dp[i-1][j+1]);
val += energy[i][j];
}
}
}
int st = 0;
for (int i = 0; i < W; i++)
if (dp[H-1][i] < dp[H-1][st])
st = i;
for (int i = H-1; i >= 0; i--) {
path[i] = st;
int v = dp[i][st] - energy[i][st];
for (Pixel *p = &data[i][st], *q = &data[i][st+1], *end = &data[i][W]; q != end; p++, q++)
*p = *q;
if (i == 0) continue;
if (st-1 >= 0 && v == dp[i-1][st-1])
st = st-1;
else if (v == dp[i-1][st])
st = st;
else
st = st+1;
}
W--;
}
void print() {
printf("%d %d\n", W, H);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j].print(), printf("%c", j == W-1 ? '\n' : ' ');
}
} test;
int main() {
int n;
scanf("%d", &n);
test.read();
test.shrink(n);
test.print();
return 0;
}