#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	string s;
	node() {}
	node(int i, int j, char c) : i(i), j(j), c(c), s(string(i + j, 'a') + c) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
queue<node> q;
char s[N][N];
int n, m, f[N][N];
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= 0; --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j]) {
				pq.push(node(i, j, s[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j];
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j];
		}
	while(true) {
		while(!pq.empty()) {
			if(q.empty() || pq.top() == q.front())
				q.push(pq.top());
			pq.pop();
		}
		if(q.front().i == n - 1 && q.front().j == n - 1) {
			cout << q.front().s << endl;
			break;
		}
		while(!q.empty()) {
			node x = q.front();q.pop();
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	string s;
	node() {}
	node(int i, int j, char c) : i(i), j(j), c(c), s(string(i + j, 'a') + c) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
queue<node> q;
char s[N][N];
int n, m, f[N][N];
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				pq.push(node(i, j, s[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j];
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j];
		}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		while(!pq.empty()) {
			if(q.empty() || pq.top() == q.front())
				q.push(pq.top());
			pq.pop();
		}
		if(q.front().i == n - 1 && q.front().j == n - 1) {
			cout << q.front().s << endl;
			break;
		}
		while(!q.empty()) {
		node x = q.front();q.pop();
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	bool last;
	node() {}
	node(int i, int j, char c, bool last) : i(i), j(j), c(c), last(last) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
node q[N << 2];
int front, back;
char s[N][N];
int n, m, f[N][N];
bool last[N][N];
void Print(int i, int j) {
	if(i || j)
		if(last[i][j])
			Print(i - 1, j);
		else
			Print(i, j - 1);
	putchar(s[i][j]);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				pq.push(node(i, j, s[i][j], last[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j], last[i + 1][j] = true;
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j], last[i][j + 1] = false;
		}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		while(!pq.empty()) {
			if(front == back || pq.top() == q[front])
				q[back++] = pq.top(), back = back == N << 2 ? 0 : back;
			pq.pop();
		}
		if(q[front].i == n - 1 && q[front].j == n - 1) {
			last[n - 1][n - 1] = q[front].last;
			Print(n - 1, n - 1);
			puts("");
			break;
		}
		if(q[front].i > n - 1 || q[front].j > n - 1)
			break;
		while(front < back) {
			node x = q[front++];front = front == N << 2 ? 0 : front;
			last[x.i][x.j] = x.last;
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.last = true;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.last = false;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 100005
using namespace std;
using ll = long long;

int n, m, a[11][N], b[11], len[N], pos[11][N];
ll ans;
bool h[N];
int main() {
	scanf("%d%d", &n, &m);
	for(int j = 1; j <= m; ++j)
		for(int i = 1; i <= n; ++i)
			scanf("%d", a[j] + i), pos[j][a[j][i]] = i;
	if(m == 1)
		return printf("%lld\n", (n + 1ll) * n / 2), 0;
	for(int i = 1; i <= n; ++i)
		if(!h[i]) {
			h[i] = true;
			for(int j = 1; j <= m; ++j)
				b[j] = pos[j][i];
			for(int k = 1; k <= n; ++k) {
				bool flag = true;
				for(int j = 2; j <= m; ++j)
					if(b[j] + k > n || a[j][b[j] + k] != a[1][b[1] + k]) {
						flag = false;
						break;
					}
				if(!flag) {
					len[i] = k;
					ans += (k + 1ll) * k / 2;
					break;
				}
				if(h[a[1][b[1] + k]]) {
					len[i] = k + len[a[1][b[1] + k]];
					ans += (len[i] + 1ll) * len[i] / 2 - (len[a[1][b[1] + k]] + 1ll) * len[a[1][b[1] + k]] / 2;
					break;
				}
				h[a[1][b[1] + k]] = true;
			}
		}
	printf("%lld\n", ans);
	return 0;
}


#include <bits/extc++.h>
#define N 2005
#define mod(x) ((x) = ((x) == N << 3 ? 0 : (x)))
using namespace std;

struct node {
	int i, j;
	char c;
	bool last;
	node() {}
	node(int i, int j, char c, bool last) : i(i), j(j), c(c), last(last) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
node q[2][N << 3];
int front[2], back[2];
char s[N][N];
int n, m, f[N][N];
bool last[N][N];
void Print(int i, int j) {
	if(i || j) {
		if(last[i][j])
			Print(i - 1, j);
		else
			Print(i, j - 1);
	}
	putchar(s[i][j]);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k) {
		bool flag = true;
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				q[0][back[0]++] = node(i, j, s[i][j], last[i][j]), mod(back[0]);
				continue;
			}
			flag = false;
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j], last[i + 1][j] = true;
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j], last[i][j + 1] = false;
		}
		if(flag)
			break;
	}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		if(front[0] != back[0]) {
			int maxx = front[0];
			for(int i = front[0] + 1; i != back[0]; ++i, mod(i))
				if(q[0][maxx] < q[0][i])
					maxx = i;
			q[1][back[1]++] = q[0][maxx], mod(back[1]);
			for(;front[0] != back[0]; ++front[0], mod(front[0]))
				if(q[0][front[0]] == q[0][maxx])
					q[1][back[1]++] = q[0][front[0]], mod(back[1]);
		}else
			return -1;
		if(q[1][front[1]].i == n - 1 && q[1][front[1]].j == n - 1) {
			last[n - 1][n - 1] = q[1][front[1]].last;
			Print(n - 1, n - 1);
			puts("");
			break;
		}
		while(front[1] != back[1]) {
			node x = q[1][front[1]++];mod(front[1]);
			last[x.i][x.j] = x.last;
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.last = true;
				y.c = s[y.i][y.j];
				q[0][back[0]++] = y, mod(back[0]);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.last = false;
				y.c = s[y.i][y.j];
				q[0][back[0]++] = y, mod(back[0]);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	string s;
	node() {}
	node(int i, int j, char c) : i(i), j(j), c(c), s(string(i + j, 'a') + c) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this);
	}
};
priority_queue<node> pq;
queue<node> q;
char s[N][N];
int n, m, f[N][N];
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= 0; --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j]) {
				pq.push(node(i, j, s[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j];
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j];
		}
	while(true) {
		while(!pq.empty()) {
			if(q.empty() || pq.top() == q.front())
				q.push(pq.top());
			pq.pop();
		}
		if(q.front().i == n - 1 && q.front().j == n - 1) {
			cout << q.front().s << endl;
			break;
		}
		while(!q.empty()) {
			node x = q.front();q.pop();
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	bool last;
	node() {}
	node(int i, int j, char c, bool last) : i(i), j(j), c(c), last(last) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
node q[N << 2];
int front, back;
char s[N][N];
int n, m, f[N][N];
bool last[N][N];
void Print(int i, int j) {
	if(i || j)
		if(last[i][j])
			Print(i - 1, j);
		else
			Print(i, j - 1);
	putchar(s[i][j]);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				pq.push(node(i, j, s[i][j], last[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j], last[i + 1][j] = true;
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j], last[i][j + 1] = false;
		}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		while(!pq.empty()) {
			if(front == back || pq.top() == q[front])
				q[back++] = pq.top();
			pq.pop();
		}
		if(q[front].i == n - 1 && q[front].j == n - 1) {
			last[n - 1][n - 1] = q[front].last;
			Print(n - 1, n - 1);
			puts("");
			break;
		}
		while(front < back) {
			node x = q[front++];
			last[x.i][x.j] = x.last;
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.last = true;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.last = false;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
#define mod(x) ((x) = ((x) == N << 2 ? 0 : (x)))
using namespace std;

struct node {
	int i, j;
	char c;
	bool last;
	node() {}
	node(int i, int j, char c, bool last) : i(i), j(j), c(c), last(last) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
node q[2][N << 2];
int front[2], back[2];
char s[N][N];
int n, m, f[N][N];
bool last[N][N];
void Print(int i, int j) {
	if(i || j) {
		if(last[i][j])
			Print(i - 1, j);
		else
			Print(i, j - 1);
	}
	putchar(s[i][j]);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k) {
		bool flag = true;
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				q[0][back[0]++] = node(i, j, s[i][j], last[i][j]), mod(back[0]);
				continue;
			}
			flag = false;
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j], last[i + 1][j] = true;
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j], last[i][j + 1] = false;
		}
		if(flag)
			break;
	}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		if(front[0] != back[0]) {
			int maxx = front[0];
			for(int i = front[0] + 1; i != back[0]; ++i, mod(i))
				if(q[0][maxx] < q[0][i])
					maxx = i;
			q[1][back[1]++] = q[0][maxx], mod(back[1]);
			for(;front[0] != back[0]; ++front[0], mod(front[0]))
				if(q[0][front[0]] == q[0][maxx])
					q[1][back[1]++] = q[0][front[0]], mod(back[1]);
		}else
			return -1;
		if(q[1][front[1]].i == n - 1 && q[1][front[1]].j == n - 1) {
			last[n - 1][n - 1] = q[1][front[1]].last;
			Print(n - 1, n - 1);
			puts("");
			break;
		}
		while(front[1] != back[1]) {
			node x = q[1][front[1]++];mod(front[1]);
			last[x.i][x.j] = x.last;
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.last = true;
				y.c = s[y.i][y.j];
				q[0][back[0]++] = y, mod(back[0]);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.last = false;
				y.c = s[y.i][y.j];
				q[0][back[0]++] = y, mod(back[0]);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	char c;
	string s;
	node() {}
	node(int i, int j, char c) : i(i), j(j), c(c), s(string(i + j, 'a') + c) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
node q[N << 3];
int front, back;
char s[N][N];
int n, m, f[N][N];
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				pq.push(node(i, j, s[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j];
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j];
		}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		while(!pq.empty()) {
			if(front == back || pq.top() == q[front])
				q[back++] = pq.top(), back = back == N << 3 ? 0 : back;
			pq.pop();
		}
		if(q[front].i == n - 1 && q[front].j == n - 1) {
			cout << q[front].s << endl;
			break;
		}
		while(front != back) {
		node x = q[front++];
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.s += y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int f;
	char c;
	bool last;
	node() : f(0), c('z' + 1), last(false) {}
	node(int f, char c, bool last) : f(f), c(c), last(last) {}
	bool operator < (const node &m) const{
		return c == m.c ? f < m.f : c > m.c;
	}
}f[N][N];
char s[N][N];
int n, m;
void Print(int i, int j) {
	if(i || j) {
		if(f[i][j].last)
			Print(i - 1, j);
		else
			Print(i, j - 1);
	}
	putchar(f[i][j].c);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	f[0][0] = m && s[0][0] != 'a' ? node(m - 1, 'a', 0) : node(m, s[0][0], 0);
	for(int k = 1; k < 2 * n - 1; ++k) {
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if((i ? f[i - 1][j] : node()) < (j ? f[i][j - 1] : node()))
				f[i][j] = j ? f[i][j - 1] : node(), f[i][j].last = false;
			else
				f[i][j] = i ? f[i - 1][j] : node(), f[i][j].last = true;
			if(f[i][j].c != 'z' + 1)
				f[i][j].c = s[i][j];
			if(f[i][j].f && f[i][j].c != 'a')
				--f[i][j].f, f[i][j].c = 'a';
		}
		int maxx = min(k, n - 1);
		for(int i = maxx - 1; i >= max(0, k - n - 1); --i)
			if(f[maxx][k - maxx].c > f[i][k - i].c)
				maxx = i;
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i)
			if(f[i][k - i].c != f[maxx][k - maxx].c)
				f[i][k - i] = node();
	}
	Print(n - 1, n - 1);
	puts("");
	return 0;
}


#include <bits/extc++.h>
#define N 2005
using namespace std;

struct node {
	int i, j;
	bool last;
	char c;
	node() {}
	node(int i, int j, char c) : i(i), j(j), c(c) {}
	bool operator < (const node &m) const{
		return i + j == m.i + m.j ? c > m.c : i + j < m.i + m.j;
	}
	bool operator == (const node &m) const{
		return !(*this < m) && !(m < *this) && i != m.i;
	}
};
priority_queue<node> pq;
node q[N << 2];
int front, back;
char s[N][N];
int n, m, f[N][N];
bool last[N][N];
void Print(int i, int j) {
	if(i || j)
		if(last[i][j])
			Print(i - 1, j);
		else
			Print(i, j - 1);
	putchar(s[i][j]);
}
int main() {
	//freopen("D.in", "r", stdin);
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; ++i)
		scanf("%s", s[i]);
	memset(f, 0x80, sizeof(f));
	f[0][0] = m;
	for(int k = 0; k < 2 * n - 1; ++k)
		for(int i = min(k, n - 1); i >= max(0, k - n + 1); --i) {
			int j = k - i;
			if(f[i][j] < 0)
				continue;
			if(!f[i][j] && s[i][j] != 'a') {
				pq.push(node(i, j, s[i][j]));
				continue;
			}
			if(s[i][j] != 'a')
				--f[i][j], s[i][j] = 'a';
			if(i < n - 1 && f[i + 1][j] < f[i][j])
					f[i + 1][j] = f[i][j], last[i + 1][j] = true;
			if(j < n - 1 && f[i][j + 1] < f[i][j])
					f[i][j + 1] = f[i][j], last[i][j + 1] = false;
		}
	if(f[n - 1][n - 1] >= 0)
		return cout << string(2 * n - 1, 'a') << endl, 0;
	while(true) {
		while(!pq.empty()) {
			if(front == back || pq.top() == q[front])
				q[back++] = pq.top();
			pq.pop();
		}
		if(q[front].i == n - 1 && q[front].j == n - 1) {
			last[n - 1][n - 1] = q[front].last;
			Print(n - 1, n - 1);
			puts("");
			break;
		}
		while(front < back) {
			node x = q[front++];
			last[x.i][x.j] = x.last;
			if(x.i < n - 1) {
				node y = x;
				++y.i;
				y.last = true;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
			if(x.j < n - 1) {
				node y = x;
				++y.j;
				y.last = false;
				y.c = s[y.i][y.j];
				pq.push(y);
			}
		}
	}
	return 0;
}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif

signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=3;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        queue<pii> q;
        char in[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        string a="";
        q.push(mp(0,0));
        queue<pii> dusriq;
        bool flag;
        int cnt=0;
        while(!q.empty()){
            cnt++;
            char mi='z';
            flag=false;
            while(!q.empty()){
                pii x=q.front();
                q.pop();
                dusriq.push(x);
                mi=min(mi,in[x.F][x.S]);
            }
            if(k>0 && mi!='a'){
                a+="a";
                flag=true;
                k--;
            }
            else a+=mi;
            if(flag){
                while(!dusriq.empty()){
                    pii x=dusriq.front();
                    dusriq.pop();
                    if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                    if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                }
            }
            else{
                while(!dusriq.empty()){
                    pii x=dusriq.front();
                    dusriq.pop();
                    if(in[x.F][x.S]==mi){
                        if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                        if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                    }
                }
            }
        }
        // cout<<cnt;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif
int dp[2005][2005];
char in[2005][2005];
signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        // char in[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // free dynamically allocated memory
        // for( int i = 0 ; i <n ; i++ )
        // {
        //     delete[] dp[i]; // delete array within matrix
        // }
        // // delete actual matrix
        // delete[] dp;
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        set<pii> unique;
        char mi='z';
        if(que.size()==0){
            q.push(mp(0,0));
            mi=in[0][0];
        }
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        pii x;
        while(!q.empty()){
            unique.clear();
            a+=mi;
            mi='z';
            while(!q.empty()){
                x=q.front();
                q.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif

signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        char in[n][n];
        int dp[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        if(que.size()==0) q.push(mp(0,0));
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
            }
            qwert=true;
            if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            // cout<<q.size();
        }
        queue<pii> dusriq;
        bool flag;
        // int cnt=0;
        while(!q.empty()){
            // cnt++;
            char mi='z';
            flag=false;
            while(!q.empty()){
                pii x=q.front();
                q.pop();
                dusriq.push(x);
                mi=min(mi,in[x.F][x.S]);
            }
            if(k>0 && mi!='a' && qwert==false){
                a+="a";
                flag=true;
                k--;
            }
            else a+=mi;
            if(flag){
                pii x;
                while(!dusriq.empty()){
                    x=dusriq.front();
                    dusriq.pop();
                    if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                }
                if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            }
            else{
                while(!dusriq.empty()){
                    pii x=dusriq.front();
                    pii sm=q.front();
                    dusriq.pop();
                    if(in[x.F][x.S]==mi){
                        if(x.F<(n-1) && (sm.F!=(x.F+1) && sm.S!=x.S)) q.push(mp(x.F+1,x.S));
                        if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                    }
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif

signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        char in[n][n];
        int dp[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // free dynamically allocated memory
        // for( int i = 0 ; i <n ; i++ )
        // {
        //     delete[] dp[i]; // delete array within matrix
        // }
        // // delete actual matrix
        // delete[] dp;
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        if(que.size()==0) q.push(mp(0,0));
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
            }
            qwert=true;
            if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            // cout<<q.size();
        }
        queue<pii> dusriq;
        bool flag;
        char mi;
        pii x;
        pii sm;
        // int cnt=0;
        while(!q.empty()){
            // cnt++;
            mi='z';
            flag=false;
            while(!q.empty()){
                x=q.front();
                q.pop();
                dusriq.push(x);
                mi=min(mi,in[x.F][x.S]);
            }
            if(k>0 && mi!='a' && qwert==false){
                a+="a";
                flag=true;
                k--;
            }
            else a+=mi;
            if(flag){
                while(!dusriq.empty()){
                    x=dusriq.front();
                    dusriq.pop();
                    if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                }
                if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            }
            else{
                while(!dusriq.empty()){
                    x=dusriq.front();
                    sm=q.front();
                    dusriq.pop();
                    if(in[x.F][x.S]==mi){
                        if(x.F<(n-1) && (sm.F!=(x.F+1) && sm.S!=x.S)) q.push(mp(x.F+1,x.S));
                        if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                    }
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif

signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        char in[n][n];
        int dp[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        if(que.size()==0) q.push(mp(0,0));
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
            }
            qwert=true;
            if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            cout<<q.size();
        }
        queue<pii> dusriq;
        bool flag;
        // int cnt=0;
        while(!q.empty()){
            // cnt++;
            char mi='z';
            flag=false;
            while(!q.empty()){
                pii x=q.front();
                q.pop();
                dusriq.push(x);
                mi=min(mi,in[x.F][x.S]);
            }
            if(k>0 && mi!='a' && qwert==false){
                a+="a";
                flag=true;
                k--;
            }
            else a+=mi;
            if(flag){
                pii x;
                while(!dusriq.empty()){
                    x=dusriq.front();
                    dusriq.pop();
                    if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                }
                if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            }
            else{
                while(!dusriq.empty()){
                    pii x=dusriq.front();
                    pii sm=q.front();
                    dusriq.pop();
                    if(in[x.F][x.S]==mi){
                        if(x.F<(n-1) && (sm.F!=(x.F+1) && sm.S!=x.S)) q.push(mp(x.F+1,x.S));
                        if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                    }
                }
            }
        }
        // cout<<cnt;
        cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif
int dp[2005][2005];
char in[2005][2005];
signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        // char in[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)>=(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)>=(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        set<pii> unique;
        char mi='z';
        if(que.size()==0){
            q.push(mp(0,0));
            mi=in[0][0];
        }
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            // cout<<a<<endl;
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        pii x;
        while(!q.empty()){
            unique.clear();
            a+=mi;
            mi='z';
            while(!q.empty()){
                x=q.front();
                q.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif
int dp[2005][2005];
char in[2005][2005];
signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        // char in[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // free dynamically allocated memory
        // for( int i = 0 ; i <n ; i++ )
        // {
        //     delete[] dp[i]; // delete array within matrix
        // }
        // // delete actual matrix
        // delete[] dp;
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        if(que.size()==0) q.push(mp(0,0));
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
            }
            qwert=true;
            if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            // cout<<q.size();
        }
        queue<pii> dusriq;
        bool flag;
        char mi;
        pii x;
        pii sm;
        // int cnt=0;
        while(!q.empty()){
            // cnt++;
            mi='z';
            flag=false;
            while(!q.empty()){
                x=q.front();
                q.pop();
                dusriq.push(x);
                mi=min(mi,in[x.F][x.S]);
            }
            if(k>0 && mi!='a' && qwert==false){
                a+="a";
                flag=true;
                k--;
            }
            else a+=mi;
            if(flag){
                while(!dusriq.empty()){
                    x=dusriq.front();
                    dusriq.pop();
                    if(x.F<(n-1)) q.push(mp(x.F+1,x.S));
                }
                if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
            }
            else{
                while(!dusriq.empty()){
                    x=dusriq.front();
                    sm=q.front();
                    dusriq.pop();
                    if(in[x.F][x.S]==mi){
                        if(x.F<(n-1) && (sm.F!=(x.F+1) && sm.S!=x.S)) q.push(mp(x.F+1,x.S));
                        if(x.S<(n-1)) q.push(mp(x.F,x.S+1));
                    }
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <iostream>
#include <fstream>
#include <cstdio>
#include <set>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <string>
#include <map>
#include <cstring>
#include <numeric>
#include <stack>
#include <iomanip>
using namespace std;

#define inf 200000000000000
#define mod 1000000007
#define eps 1e-7
#define PI 3.1415926535897932385

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define sl(mark) scanf("%lld",&mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#define mp make_pair
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vp;

// #define int ll

#ifndef hritvi
#define endl '\n'
#define start_routine()
#define end_routine()
#endif
int dp[2005][2005];
char in[2005][2005];
signed main() {
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(0);
    int t=1;
    #ifdef hritvi
        t=1;
    #endif
    while(t--){
        int n,k;
        cin>>n>>k;
        // queue<pii> q;
        // char in[n][n];
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++) cin>>in[i][j];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==0 && j==0){
                    if(in[0][0]=='a') dp[0][0]=1;
                    else dp[0][0]=0;
                }
                else if(j==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i-1][j]+1;
                    else dp[i][j]=dp[i-1][j];
                }
                else if(i==0){
                    if(in[i][j]=='a') dp[i][j]=dp[i][j-1]+1;
                    else dp[i][j]=dp[i][j-1];
                }
                else{
                    if(in[i][j]=='a') dp[i][j]=max(dp[i-1][j],dp[i][j-1])+1;
                    else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
                }
            }
        }
        // free dynamically allocated memory
        // for( int i = 0 ; i <n ; i++ )
        // {
        //     delete[] dp[i]; // delete array within matrix
        // }
        // // delete actual matrix
        // delete[] dp;
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<n;j++){
        //         cout<<dp[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        queue<pii> que;
        for(int sum=0;sum<n-1;sum++){
            for(int j=0;j<=sum;j++){
                int i=sum-j;
                if((dp[n-i-1][n-j-1]+k)==(n-i-1+n-j)) que.push(mp(n-i-1,n-j-1));
            }
            if(que.size()>0) break;
        }
        if(que.size()==0){
            for(int sum=n-1;sum>=0;sum--){
                for(int j=0;j<=sum;j++){
                    int i=sum-j;
                    if((dp[i][j]+k)==(i+j+1)) que.push(mp(i,j));
                }
                if(que.size()>0) break;
            }
        }
        string a="";
        bool qwert=false;
        queue<pii> q;
        set<pii> unique;
        char mi='z';
        if(que.size()==0) q.push(mp(0,0));
        else{
            pii x=que.front();
            int z=x.F+x.S;
            // cout<<z;
            for(int i=0;i<=z;i++){
                a+="a";
            }
            while(!que.empty()){
                x=que.front();
                que.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        pii x;
        while(!q.empty()){
            unique.clear();
            a+=mi;
            mi='z';
            while(!q.empty()){
                x=q.front();
                q.pop();
                if(x.F<(n-1)){
                    unique.insert(mp(x.F+1,x.S));
                    mi=min(in[x.F+1][x.S],mi);
                }
                if(x.S<(n-1)){
                    unique.insert(mp(x.F,x.S+1));
                    mi=min(in[x.F][x.S+1],mi);
                }
            }
            for(auto itr=unique.begin();itr!=unique.end();++itr){
                if(in[(*itr).F][(*itr).S]==mi){
                    q.push(*itr);
                }
            }
        }
        // cout<<cnt;
        // cout<<endl;
        cout<<a<<endl;

    }
    end_routine();

}


#include <bits/stdc++.h>

#define endl "\n"
#define int char
using ll = long long;
using namespace std;

int dp[2000][2000];
string s[2000];
int n;

char f(int x, int y) {
    char res = 'z';
    if (x < n - 1) {
        res = s[x + 1][y];
    }
    if (y < n - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j) < f(x, y)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<pair<int, int>> kek = {{x, y}};
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using ll = long long;
using namespace std;

int dp[2000][2000];
string s[2000];
int n;

char f(int x, int y) {
    char res = 'z';
    if (x < n - 1) {
        res = s[x + 1][y];
    }
    if (y < n - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j) < f(x, y)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<pair<int, int>> kek = {{x, y}};
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

char s[2000][2000];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> s[i][j];
    }
    vector<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        ndp.shrink_to_fit();
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.emplace_back(i, j);
                } else if (dist == mxd) {
                    kek.emplace_back(i, j);
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        cout << 'a';
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        kek.shrink_to_fit();
        now.clear();
        if (!kek.empty()) {
            cout << mn;
        }
    }
    cout << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < s.size() - 1) {
        res = s[x][y + 1];
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    int dp[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 0;
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    while (x < n - 1 || y < n - 1) {
        if (x == -1) {
            res.push_back(s[x + 1][y]);
            x++;
        } else if (x == n - 1) {
            res.push_back(s[x][y + 1]);
            y++;
        } else if (y == n - 1) {
            res.push_back(s[x + 1][y]);
            x++;
        } else {
            res.push_back(min(s[x + 1][y], s[x][y + 1]));
            if (res.back() == s[x + 1][y]) {
                x++;
            } else {
                y++;
            }
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    int dp[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 0;
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    while (x < n - 1 || y < n - 1) {
        if (x == -1) {
            res.push_back(s[x + 1][y]);
            x++;
        } else if (x == n - 1) {
            res.push_back(s[x][y + 1]);
            y++;
        } else if (y == n - 1) {
            res.push_back(s[x + 1][y]);
            x++;
        } else {
            res.push_back(min(s[x + 1][y], s[x][y + 1]));
            if (res.back() == s[x + 1][y]) {
                x++;
            } else {
                y++;
            }
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < (int) s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < (int) s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int dp[2000][2000];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1 && cell.x >= 0) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek.clear();
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

char s[2000][2000];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> s[i][j];
    }
    vector<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.emplace_back(i, j);
                } else if (dist == mxd) {
                    kek.emplace_back(i, j);
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        cout << 'a';
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            cout << mn;
        }
    }
    cout << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using ll = long long;
using namespace std;

int dp[2000][2000];
string s[2000];
int n;

char f(int x, int y) {
    char res = 'z';
    if (x < n - 1) {
        res = s[x + 1][y];
    }
    if (y < n - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j) < f(x, y)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1 && cell.x >= 0) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek.clear();
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    int dp[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            dp[i][j] = 0;
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek = {};
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

int dp[2000][2000];
string s[2000];
int n;

char f(int x, int y) {
    char res = 'z';
    if (x < n - 1) {
        res = s[x + 1][y];
    }
    if (y < n - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j) < f(x, y)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1 && cell.x >= 0) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek.clear();
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

int dp[2000][2000];
string s[2000];
int n;

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<pair<int, int>> kek = {};
    if (mxd == 0) {
        kek.emplace_back(-1, 0);
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist == mxd) {
                kek.emplace_back(i, j);
            }
        }
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < (int) s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < (int) s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int dp[2000][2000];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek = {};
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

char s[2000][2000];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> s[i][j];
    }
    set<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        ndp.shrink_to_fit();
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.insert({i, j});
                } else if (dist == mxd) {
                    kek.insert({i, j});
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        cout << 'a';
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.insert(cell);
            }
        }
        if (!kek.empty()) {
            cout << mn;
        }
    }
    cout << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

string s[2000];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        cin >> s[i];
    }
    vector<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.emplace_back(i, j);
                } else if (dist == mxd) {
                    kek.emplace_back(i, j);
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < (int) s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < (int) s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int dp[2000][2000];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1 && cell.x >= 0) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek = {};
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
using ll = long long;
using namespace std;

char f(int x, int y, vector<string> &s) {
    char res = 'z';
    if (x < s.size() - 1) {
        res = s[x + 1][y];
    }
    if (y < s.size() - 1) {
        res = min(res, s[x][y + 1]);
    }
    return res;
}

struct trio {
    char c;
    int x, y;
};

int dp[2000][2000];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    vector<string> s(n);
    for (auto &x : s) {
        cin >> x;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                dp[i][j] = dp[i - 1][j];
            }
            if (j > 0) {
                dp[i][j] = max(dp[i][j], dp[i][j - 1]);
            }
            if (s[i][j] == 'a') {
                dp[i][j]++;
            }
        }
    }
    int mxd = 0, x = -1, y = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int dist = i + j + 1;
            if (dp[i][j] + k >= dist && dist > mxd) {
                mxd = dist;
                x = i, y = j;
            } else if (dp[i][j] + k >= dist && dist == mxd) {
                if (f(i, j, s) < f(x, y, s)) {
                    x = i, y = j;
                }
            }
        }
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    vector<trio> kek = {{'a', x, y}};
    while (!kek.empty()) {
        vector<trio> now;
        for (auto &cell : kek) {
            if (cell.x < n - 1) {
                now.push_back({s[cell.x + 1][cell.y], cell.x + 1, cell.y});
            }
            if (cell.y < n - 1) {
                now.push_back({s[cell.x][cell.y + 1], cell.x, cell.y + 1});
            }
        }
        char mn = 'z';
        for (auto &cell : now) {
            mn = min(mn, cell.c);
        }
        kek = {};
        for (auto &cell : now) {
            if (cell.c == mn) {
                kek.push_back(cell);
            }
        }
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

char s[2000][2000];

signed main() {
//    ios::sync_with_stdio(false);
//    cin.tie(nullptr);
//    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> s[i][j];
    }
    vector<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.emplace_back(i, j);
                } else if (dist == mxd) {
                    kek.emplace_back(i, j);
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>

#define endl "\n"
#define int short int
using namespace std;

char s[2000][2000];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n, k;
    cin >> n >> k;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++)
            cin >> s[i][j];
    }
    vector<pair<int, int>> kek = {{-1, 0}};
    int mxd = 0;
    vector<int> dp(n);
    for (int i = 0; i < n; i++) {
        vector<int> ndp(n);
        for (int j = 0; j < n; j++) {
            if (i > 0) {
                ndp[j] = dp[j];
            }
            if (j > 0) {
                ndp[j] = max(ndp[j], ndp[j - 1]);
            }
            if (s[i][j] == 'a') {
                ndp[j]++;
            }
            int dist = i + j + 1;
            if (ndp[j] + k >= dist) {
                if (dist > mxd) {
                    mxd = dist;
                    kek.clear();
                    kek.emplace_back(i, j);
                } else if (dist == mxd) {
                    kek.emplace_back(i, j);
                }
            }
        }
        dp = ndp;
    }
    string res;
    for (int i = 0; i < mxd; i++) {
        res.push_back('a');
    }
    while (!kek.empty()) {
        vector<pair<int, int>> now;
        char mn = 'z';
        for (auto &cell : kek) {
            if (cell.first < n - 1) {
                now.emplace_back(cell.first + 1, cell.second);
                mn = min(mn, s[cell.first + 1][cell.second]);
            }
            if (cell.second < n - 1 && cell.first >= 0) {
                now.emplace_back(cell.first, cell.second + 1);
                mn = min(mn, s[cell.first][cell.second + 1]);
            }
        }
        kek.clear();
        for (auto &cell : now) {
            if (s[cell.first][cell.second] == mn) {
                kek.push_back(cell);
            }
        }
        now.clear();
        if (!kek.empty()) {
            res.push_back(mn);
        }
    }
    cout << res << endl;
}


#include <bits/stdc++.h>
#define int long long

const int inf = 1e17;

using namespace std;

int n, cnt[2005][2005], max1=2, erase1; string a[2005], ans=""; bool used[2005][2005];

signed main()
{
	//if i dont get an AC in my first submission i will propose to my crush

	//edit: fuck.

	//edit: god damn it so many dumb mistakes

	//edit: lol 4 times im debugging by submitting right now ffs. this time if gonna be an AC :>

	//edit: okey 5 times enough for today

	//edit: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

	//edit: wrong...answer...on...test.. .. 7...3
	{
		a[0]="}";
		memset(used, 0, sizeof(used));
	}

	cin>>n>>erase1;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
		a[0]+='}', a[i]='}'+a[i];
		cnt[i][0]=-inf, cnt[0][i]=-inf;
	}
	cnt[0][1]=erase1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cnt[i][j]=max(cnt[i-1][j], cnt[i][j-1])-1;
			//cout<<a[i][j];
			if(a[i][j]=='a')
			{
				cnt[i][j]++;
			}
			if(cnt[i][j]>=0)
			{
				if(i+j>max1)
				{
					max1=i+j;
				}
			}
			//cout<<cnt[i][j]<<"\t";
		}
		//cout<<endl;
	}
	//cout<<max1<<endl;
	if(erase1!=0)
	{
		for(int i=1; i<max1; i++)
		{
			ans+='a';
		}
	}
	else
	{
		ans+=a[1][1];
	}
	if(max1==2*n)
	{
		if(n==1)
		{
			cout<<a[1][1];
		}
		else for(int i=1; i<max1; i++)
		{
			cout<<"a";
		}
		return 0;
	}
	cnt[1][1]=0;
	int maxi=min(n, max1-1), mini=max1-maxi;
	for(int i=maxi; i>=mini; i--)
	{
		if(cnt[i][max1-i]==0)
		{
			a[i][max1-i]='a';
			used[i][max1-i]=true;
		}
	}
	//cout<<"debug\n";
	max1++;
	while(max1<=2*n)
	{
		char minchar='}';
		maxi=min(n, max1-1);
		mini=max1-maxi;
		for(int i=maxi; i>=mini; i--)
		{
			if(used[i-1][max1-i]||used[i][max1-i-1])
			{
				//cout<<"ha";
				minchar=min(minchar, a[i][max1-i]);
			}
		}
		for(int i=maxi; i>=mini; i--)
		{
			if(a[i][max1-i]==minchar&&(used[i-1][max1-i]||used[i][max1-i-1]))
			{
				used[i][max1-i]=true;
			}
		}
		ans+=minchar;
		max1++;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define int long long

const int inf = 1e17;

using namespace std;

int n, cnt[2005][2005], max1=2, erase1; string a[2005], ans=""; bool used[2005][2005];

signed main()
{
	//if i dont get an AC in my first submission i will propose to my crush

	//edit: fuck.

	//edit: god damn it so many dumb mistakes

	//edit: lol 4 times im debugging by submitting right now ffs. this time if gonna be an AC :>

	{
		a[0]="}";
		memset(used, 0, sizeof(used));
	}

	cin>>n>>erase1;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
		a[0]+='}', a[i]='}'+a[i];
		cnt[i][0]=-inf, cnt[0][i]=-inf;
	}
	cnt[0][1]=erase1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cnt[i][j]=max(cnt[i-1][j], cnt[i][j-1])-1;
			//cout<<a[i][j];
			if(a[i][j]=='a')
			{
				cnt[i][j]++;
			}
			if(cnt[i][j]>=0)
			{
				if(i+j>max1)
				{
					max1=i+j;
				}
			}
			//cout<<cnt[i][j]<<"\t";
		}
		//cout<<endl;
	}
	//cout<<max1<<endl;
	if(erase1!=0)
	{
		for(int i=1; i<max1; i++)
		{
			ans+='a';
		}
	}
	else
	{
		ans+=a[1][1];
	}
	if(max1==2*n)
	{
		for(int i=1; i<max1; i++)
		{
			cout<<"a";
		}
		return 0;
	}
	cnt[1][1]=0;
	int maxi=min(n, max1-1), mini=max1-maxi;
	for(int i=maxi; i>=mini; i--)
	{
		if(cnt[i][max1-i]==0)
		{
			a[i][max1-i]='a';
			used[i][max1-i]=true;
		}
	}
	//cout<<"debug\n";
	max1++;
	while(max1<=2*n)
	{
		char minchar='}';
		maxi=min(n, max1-1);
		mini=max1-maxi;
		for(int i=maxi; i>=mini; i--)
		{
			if(used[i-1][max1-i]||used[i][max1-i-1])
			{
				//cout<<"ha";
				minchar=min(minchar, a[i][max1-i]);
			}
		}
		for(int i=maxi; i>=mini; i--)
		{
			if(a[i][max1-i]==minchar)
			{
				used[i][max1-i]=true;
			}
		}
		ans+=minchar;
		max1++;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define int long long

const int inf = 1e17;

using namespace std;

int n, cnt[2005][2005], max1=1, erase1; string a[2005], ans=""; bool trail[2005][2005];

signed main()
{
	//if i dont get an AC in my first submission i will propose to my crush

	{
		a[0]="}";
	}

	cin>>n>>erase1;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
		a[0]+='}', a[i]='}'+a[i];
		cnt[i][0]=-inf, cnt[0][i]=-inf;
	}
	cnt[0][1]=erase1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cnt[i][j]=max(cnt[i-1][j], cnt[i][j-1])-1;
			//cout<<a[i][j];
			if(a[i][j]=='a')
			{
				cnt[i][j]++;
			}
			if(cnt[i][j]>=0)
			{
				if(i+j>max1)
				{
					max1=i+j;
				}
			}
			//cout<<cnt[i][j]<<"\t";
		}
		//cout<<endl;
	}
	//cout<<max1<<endl;
	for(int i=1; i<max1; i++)
	{
		ans+='a';
	}
	if(max1==2*n)
	{
		for(int i=1; i<max1; i++)
		{
			cout<<"a";
		}
		return 0;
	}
	int maxi=min(n, max1-1), mini=max1-maxi;
	for(int i=maxi; i>=mini; i--)
	{
		if(cnt[i][max1-i]==0)
		{
			a[i][max1-i]='a';
		}
	}
	//cout<<"debug\n";
	max1++;
	while(max1<=2*n)
	{
		char minchar='}';
		maxi=min(n, max1-1);
		mini=max1-maxi;
		for(int i=maxi; i>=mini; i--)
		{
			if(a[i-1][max1-i]==ans[ans.length()-1]||a[i][max1-i-1]==ans[ans.length()-1])
			{
				//cout<<"ha";
				minchar=min(minchar, a[i][max1-i]);
			}
		}
		ans+=minchar;
		max1++;
	}
	cout<<ans;
}


#include <bits/stdc++.h>
#define int long long

const int inf = 1e17;

using namespace std;

int n, cnt[2005][2005], max1=2, erase1; string a[2005], ans=""; bool used[2005][2005];

signed main()
{
	//if i dont get an AC in my first submission i will propose to my crush

	//edit: fuck.

	//edit: god damn it so many dumb mistakes

	//edit: lol 4 times im debugging by submitting right now ffs. this time if gonna be an AC :>

	//edit: okay 5 times enough for today
	{
		a[0]="}";
		memset(used, 0, sizeof(used));
	}

	cin>>n>>erase1;
	for(int i=1; i<=n; i++)
	{
		cin>>a[i];
		a[0]+='}', a[i]='}'+a[i];
		cnt[i][0]=-inf, cnt[0][i]=-inf;
	}
	cnt[0][1]=erase1;
	for(int i=1; i<=n; i++)
	{
		for(int j=1; j<=n; j++)
		{
			cnt[i][j]=max(cnt[i-1][j], cnt[i][j-1])-1;
			//cout<<a[i][j];
			if(a[i][j]=='a')
			{
				cnt[i][j]++;
			}
			if(cnt[i][j]>=0)
			{
				if(i+j>max1)
				{
					max1=i+j;
				}
			}
			//cout<<cnt[i][j]<<"\t";
		}
		//cout<<endl;
	}
	//cout<<max1<<endl;
	if(erase1!=0)
	{
		for(int i=1; i<max1; i++)
		{
			ans+='a';
		}
	}
	else
	{
		ans+=a[1][1];
	}
	if(max1==2*n)
	{
		for(int i=1; i<max1; i++)
		{
			cout<<"a";
		}
		return 0;
	}
	cnt[1][1]=0;
	int maxi=min(n, max1-1), mini=max1-maxi;
	for(int i=maxi; i>=mini; i--)
	{
		if(cnt[i][max1-i]==0)
		{
			a[i][max1-i]='a';
			used[i][max1-i]=true;
		}
	}
	//cout<<"debug\n";
	max1++;
	while(max1<=2*n)
	{
		char minchar='}';
		maxi=min(n, max1-1);
		mini=max1-maxi;
		for(int i=maxi; i>=mini; i--)
		{
			if(used[i-1][max1-i]||used[i][max1-i-1])
			{
				//cout<<"ha";
				minchar=min(minchar, a[i][max1-i]);
			}
		}
		for(int i=maxi; i>=mini; i--)
		{
			if(a[i][max1-i]==minchar&&used[i-1][max1-i]||used[i][max1-i-1])
			{
				used[i][max1-i]=true;
			}
		}
		ans+=minchar;
		max1++;
	}
	cout<<ans;
}