#include <bits/stdc++.h>
using namespace std;

int i, n, m, j;
string s;

int get_nr(char c1, char c2) {
  if(c1 >= '0' && c1 <= '9') c1 -= '0';
  else c1 += 10 - 'A';
  if(c2 >= '0' && c2 <= '9') c2 -= '0';
  else c2 += 10 - 'A';

  return c1 * 16 + c2;
}

int diff(int x, int y, int z, string s) {
  int ans = 0;

  ans += abs(get_nr(s[1], s[2]) - x * 16 - x);
  ans += abs(get_nr(s[3], s[4]) - y * 16 - y);
  ans += abs(get_nr(s[5], s[6]) - z * 16 - z);

  return ans;
}

string solve(string s) {
  int whox = 0, whoy = 0, whoz = 0;

  for(int i = 0; i < 16; ++i)
    for(int j = 0; j < 16; ++j)
      for(int k = 0; k < 16; ++k) {
        if(diff(whox, whoy, whoz, s) <= diff(i, j, k, s)) continue;
        whox = i; whoy = j; whoz = k;
      }

  string ans = "#";
  ans += (whox > 9 ? 'A' + whox - 10 : '0' + whox);
  ans += (whoy > 9 ? 'A' + whoy - 10 : '0' + whoy);
  ans += (whoz > 9 ? 'A' + whoz - 10 : '0' + whoz);

  return ans;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> m;

  for(i = 1; i <= n; ++i)
    for(j = 1; j <= m; ++j)
      cin >> s, cout << solve(s) << " \n"[j == m];

  return 0;
}