#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
int n, m;
char g[31][31];
int N;
int node[30][30];
bool used[30][30];
vector<int> G[14];
bool gem[7][7];
int ncolor[7];
int vcolor[14];
int ret;
bool outputed;
char gret[30][30];
void rec(int v) {
  if (v == N) {
    for (int i = 0, _n = (7); i < _n; ++i)
      for (int j = 0, _n = (7); j < _n; ++j)
        if (!gem[i][j]) return;
    ret++;
    if (!outputed) {
      for (int i = 0, _n = (n); i < _n; ++i)
        for (int j = 0, _n = (m); j < _n; ++j)
          gret[i][j] = node[i][j] == -1 ? '.' : '0' + vcolor[node[i][j]];
      outputed = true;
    }
    return;
  }
  for (int i = 0, _n = (7); i < _n; ++i)
    if (ncolor[i] < 2) {
      ncolor[i]++;
      vcolor[v] = i;
      for (int j = 0, _n = ((int)((G[v]).size())); j < _n; ++j) {
        int c2 = vcolor[G[v][j]];
        if (c2 != -1 && gem[i][c2]) goto nxt;
      }
      for (int j = 0, _n = ((int)((G[v]).size())); j < _n; ++j) {
        int c2 = vcolor[G[v][j]];
        if (c2 != -1) gem[i][c2] = gem[c2][i] = true;
      }
      rec(v + 1);
      for (int j = 0, _n = ((int)((G[v]).size())); j < _n; ++j) {
        int c2 = vcolor[G[v][j]];
        if (c2 != -1) gem[i][c2] = gem[c2][i] = false;
      }
    nxt:;
      vcolor[v] = -1;
      ncolor[i]--;
    }
}
void solution() {
  scanf("%d %d\n", &n, &m);
  for (int i = 0, _n = (n); i < _n; ++i) gets(g[i]);
  memset((node), (-1), sizeof((node)));
  for (int i = 0, _n = (n); i < _n; ++i)
    for (int j = 0, _n = (m); j < _n; ++j)
      if (g[i][j] != '.' && node[i][j] == -1)
        node[i][j] = node[i][j + 1] = node[i + 1][j] = node[i + 1][j + 1] = N++;
  if (N != 14) throw 1;
  for (int i = 0, _n = (n); i < _n; ++i)
    for (int j = 0, _n = (m); j < _n; ++j)
      if (g[i][j] != '.' && !used[i][j]) {
        int i2, j2;
        if (g[i + 1][j] == g[i][j])
          i2 = i + 1, j2 = j;
        else if (g[i][j + 1] == g[i][j])
          i2 = i, j2 = j + 1;
        else
          continue;
        int v = node[i][j];
        int v2 = node[i2][j2];
        G[v].push_back(v2);
        if (v2 != v) G[v2].push_back(v);
        used[i][j] = used[i2][j2] = true;
      }
  memset((vcolor), (-1), sizeof((vcolor)));
  rec(0);
  printf("%d\n", ret);
  for (int i = 0; i < n; i++, putchar('\n'))
    for (int j = 0, _n = (m); j < _n; ++j) putchar(gret[i][j]);
}
int main() {
  solution();
  return 0;
}
