#include using namespace std; typedef long long ll; const int N = 500 + 3; int h[N][N], n, m, ans[N][N]; bool vr[N][N]; array s[N * N]; array, 4> adj{array{1, 0}, {-1, 0}, {0, 1}, {0, -1}}; struct DSU{ array p[N][N], big[N][N]; int sz[N][N]; DSU(){} void init(){ for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j){ p[i][j] = {i, j}; big[i][j] = {i, j}; sz[i][j] = 1; } } array get_p(int x, int y){ if(p[x][y] == array{x, y}) return p[x][y]; return p[x][y] = get_p(p[x][y][0], p[x][y][1]); } bool unite(int x1, int y1, int x2, int y2, int val){ auto p1 = get_p(x1, y1); auto p2 = get_p(x2, y2); if(p1 == p2) return false; x1 = p1[0], y1 = p1[1]; x2 = p2[0], y2 = p2[1]; if(sz[x1][y1] < sz[x2][y2]){ swap(x1, x2); swap(y1, y2); } sz[x1][y1] += sz[x2][y2]; p[x2][y2] = {x1, y1}; auto [b1_x, b1_y] = big[x1][y1]; auto [b2_x, b2_y] = big[x2][y2]; if(h[b1_x][b1_y] < h[b2_x][b2_y]){ ans[b1_x][b1_y] = val; big[x1][y1] = big[x2][y2]; } else ans[b2_x][b2_y] = val; return true; } } dsu; bool valid(int x, int y){ if(x < 0 || x >= n || y < 0 || y >= m) return false; return true; } void input(string s){ freopen((s + ".in").c_str(), "r", stdin); freopen((s + ".out").c_str(), "w", stdout); } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); input("prominence2d"); cin >> n >> m; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) cin >> h[i][j]; for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j){ vr[i][j] = true; for(auto [dx, dy]: adj){ int newx = dx + i; int newy = dy + j; if(!valid(newx, newy)) continue; if(h[newx][newy] > h[i][j]){ vr[i][j] = false; break; } } } for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) s[i * m + j] = {i, j}; sort(s, s + n * m, [&](auto l, auto r){ return h[l[0]][l[1]] > h[r[0]][r[1]]; }); for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) ans[i][j] = 0; dsu.init(); for(int i = 0; i < n * m; ++i){ auto [x, y] = s[i]; //cout << x << " " << y << " - " << h[x][y] << endl; for(auto [dx, dy]: adj){ int newx = x + dx; int newy = y + dy; if(!valid(newx, newy)) continue; if(h[newx][newy] > h[x][y]) dsu.unite(newx, newy, x, y, h[x][y]); } } for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) if(vr[i][j]) cout << h[i][j] - ans[i][j] << "\n"; }