#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m;
vector<vector<int>> matrix;
vector<bool> visited;
vector<int> currentPath;
vector<int> longestPath;
int maxLength = 0;
// 判断坐标是否合法
bool isValid(int x, int y) {
return x >= 0 && x < n && y >= 0 && y < m;
}
// 深度优先搜索
void dfs(int x, int y, int length) {
visited[x * m + y] = true;
currentPath.push_back(matrix[x][y]);
if (length > maxLength) {
maxLength = length;
longestPath = currentPath;
} else if (length == maxLength && currentPath < longestPath) {
longestPath = currentPath;
}
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
for (int i = 0; i < 4; i++) {
int newX = x + dx[i];
int newY = y + dy[i];
if (isValid(newX, newY) &&!visited[newX * m + newY] && matrix[newX][newY] > matrix[x][y]) {
dfs(newX, newY, length + 1);
}
}
visited[x * m + y] = false;
currentPath.pop_back();
}
int main() {
cin >> n >> m;
matrix.resize(n, vector<int>(m));
visited.resize(n * m, false);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dfs(i, j, 1);
}
}
for (int num : longestPath) {
cout << num << " ";
}
cout << endl;
return 0;
}