#include <bits/stdc++.h>
using namespace std;
inline int read(void) {
  register int x = 0;
  register short sgn = 1;
  register char c = getchar();
  while (c < 48 || 57 < c) {
    if (c == 45) sgn = 0;
    c = getchar();
  }
  while (47 < c && c < 58) {
    x = (x << 3) + (x << 1) + c - 48;
    c = getchar();
  }
  return sgn ? x : -x;
}
char wrunit[19];
short wrcnt;
inline void write(int x) {
  wrcnt = 0;
  if (x == 0) {
    putchar('0');
    return;
  }
  if (x < 0) putchar('-'), x = -x;
  while (x != 0) {
    wrunit[++wrcnt] = x % 10 + 48;
    x /= 10;
  }
  for (; wrcnt > 0; --wrcnt) putchar(wrunit[wrcnt]);
}
vector<int> Edge[40050];
inline void AddEdge(int s, int e) { Edge[s].push_back(e); }
int dfn[20050], low[20050], srl[20050];
int cnt_dfn, cnt_srl;
int stk[20050], top;
bool vis[20050];
inline void tarjan(int p) {
  dfn[p] = low[p] = ++cnt_dfn;
  stk[++top] = p;
  vis[p] = true;
  int s = Edge[p].size();
  for (register int i = 0; i < s; ++i) {
    int& v = Edge[p][i];
    if (!dfn[v]) {
      tarjan(v);
      low[p] = min(low[p], low[v]);
    } else if (vis[v])
      low[p] = min(low[p], dfn[v]);
  }
  if (dfn[p] == low[p]) {
    srl[p] = ++cnt_srl;
    while (stk[top] != p) {
      srl[stk[top]] = cnt_srl;
      vis[stk[top--]] = false;
    }
    vis[stk[top--]] = false;
  }
}
int n, m;
int a[105][105];
int cnt;
inline int g(int i, int u) { return i * m + u; }
inline void work() {
  cnt_dfn = cnt_srl = 0;
  for (register int i = 0; i < 20001; ++i) dfn[i] = vis[n] = 0;
  for (register int i = 0; i < 20001; ++i) Edge[i].clear();
  top = 0;
  n = read(), m = read();
  cnt = n * m;
  for (register int i = 0; i < n; ++i)
    for (register int u = 0; u < m; ++u) a[i][u] = read();
  for (register int i = 0; i < n; ++i)
    for (register int u = 0; u < m; ++u) {
      if (u != 0) {
        if (a[i][u] == a[i][u - 1]) {
          AddEdge(g(i, u) + cnt, g(i, u - 1));
          AddEdge(g(i, u - 1) + cnt, g(i, u));
          AddEdge(g(i, u), g(i, u - 1) + cnt);
          AddEdge(g(i, u - 1), g(i, u) + cnt);
        }
        if (a[i][u] == a[i][u - 1] + 1) {
          AddEdge(g(i, u) + cnt, g(i, u - 1) + cnt);
          AddEdge(g(i, u - 1), g(i, u));
        }
        if (a[i][u] == a[i][u - 1] - 1) {
          AddEdge(g(i, u), g(i, u - 1));
          AddEdge(g(i, u - 1) + cnt, g(i, u) + cnt);
        }
      }
      if (i != 0) {
        if (a[i][u] == a[i - 1][u]) {
          AddEdge(g(i, u) + cnt, g(i - 1, u));
          AddEdge(g(i - 1, u) + cnt, g(i, u));
          AddEdge(g(i, u), g(i - 1, u) + cnt);
          AddEdge(g(i - 1, u), g(i, u) + cnt);
        }
        if (a[i][u] == a[i - 1][u] + 1) {
          AddEdge(g(i, u) + cnt, g(i - 1, u) + cnt);
          AddEdge(g(i - 1, u), g(i, u));
        }
        if (a[i][u] == a[i - 1][u] - 1) {
          AddEdge(g(i, u), g(i - 1, u));
          AddEdge(g(i - 1, u) + cnt, g(i, u) + cnt);
        }
      }
    }
  for (register int i = 0; i < (cnt << 1); ++i) {
    if (!dfn[i]) tarjan(i);
  }
  for (register int i = 0; i < n; ++i) {
    for (register int u = 0; u < m; ++u)
      write((srl[g(i, u)] < srl[g(i, u) + cnt] ? 1 : 0) + a[i][u]),
          putchar(' ');
    puts("");
  }
}
signed main() {
  int T = read();
  while (T--) work();
  return 0;
}
