#include #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define rep2(i, l, r) for (int i = (l); i < (r); i++) #define rep2r(i, l, r) for (int i = (r) - 1; i >= (l); i--) #define range(a) a.begin(), a.end() using namespace std; using ll = long long; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(15); int H, W; cin >> H >> W; vector A(H); rep(i, H) cin >> A[i]; vector B(W); rep(i, W) cin >> B[i]; vector row(H * W, -1); vector col(H * W, -1); auto index = [&](int i, int j) { return i * W + j; }; rep(i, H) { if (A[i] == 1) { repr(j, W - 1) row[index(i, j + 1)] = index(i, j); } else { rep(j, A[i] - 2) row[index(i, j)] = index(i, j + 1); row[index(i, A[i] - 2)] = index(i, W - 1); rep2r(j, A[i] - 1, W - 1) row[index(i, j + 1)] = index(i, j); } } rep(j, W) { if (B[j] == 1) { repr(i, H - 1) col[index(i + 1, j)] = index(i, j); } else { rep(i, B[j] - 2) col[index(i, j)] = index(i + 1, j); col[index(B[j] - 2, j)] = index(H - 1, j); rep2r(i, B[j] - 1, H - 1) col[index(i + 1, j)] = index(i, j); } } vector ans(H * W, -1); int now = H * W; auto dfs = [&](auto dfs, int u) -> void { if (u == -1) return; if (ans[u] != -1) return; dfs(dfs, row[u]); dfs(dfs, col[u]); ans[u] = now--; }; rep(i, H * W) if (ans[i] == -1) dfs(dfs, i); rep(i, H) rep(j, W) cout << ans[index(i, j)] << " \n"[j == W - 1]; }