#include <iostream>
using namespace std;
const int MAX_SIZE = 50;
int main() {
int line, col, mt[MAX_SIZE + 1][MAX_SIZE + 1];
cin >> line >> col;
// Citirea matricei
for (int i = 1; i <= line; ++i) {
for (int j = 1; j <= col; ++j) {
cin >> mt[i][j];
}
}
bool found = false; // Variabila pentru a verifica daca s-a gasit ceva
for (int i = 1; i <= (line + col - 2) / 2; ++i) {
int safePlace = 0;
// Parcurgerea marginii superioare
for (int j = i; j <= col - i + 1; ++j) {
if (safePlace < mt[i][j]) {
safePlace = mt[i][j];
found = true;
cout << mt[i][j] << " ";
}
}
// Parcurgerea marginii drepte
for (int j = i + 1; j <= line - i; ++j) {
if (safePlace < mt[j][col - i + 1]) {
safePlace = mt[j][col - i + 1];
found = true;
cout << mt[j][col - i + 1] << " ";
}
}
// Parcurgerea marginii inferioare
for (int j = col - i + 1; j >= i; --j) {
if (safePlace < mt[line - i + 1][j]) {
safePlace = mt[line - i + 1][j];
found = true;
cout << mt[line - i + 1][j] << " ";
}
}
// Parcurgerea marginii stângi
for (int j = line - i; j > i; --j) {
if (safePlace < mt[j][i]) {
safePlace = mt[j][i];
found = true;
cout << mt[j][i] << " ";
}
}
}
// Afișarea rezultatului
if (found) {
cout << "DA";
} else {
cout << "NU";
}
return 0;
}