#include <bits/stdc++.h>
using namespace std;
const int N = 35;
int dx[4] = {-1, 1, 0, 0};
int dy[4] = {0, 0, -1, 1};
int n, m, kp, Map[N][N], exist[105];
char ch[N][N];
pair<int, int> pos[25], adj[N][N];
bool vis[25], vis2[N][N];
long long ans;
int ansMap[N][N];
bool place(int r, int c, int k) {
  bool flag = true;
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j) Map[i][j] = k;
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j)
      if (Map[adj[i][j].first][adj[i][j].second] != -1) {
        if (adj[i][j].first == r || adj[i][j].first == r + 1)
          if (adj[i][j].second == c || adj[i][j].second == c + 1)
            if (i <= adj[i][j].first && j <= adj[i][j].second) continue;
        int lb = Map[i][j], rb = Map[adj[i][j].first][adj[i][j].second];
        if (exist[min(lb, rb) * 10 + max(lb, rb)]) flag = false;
        exist[min(lb, rb) * 10 + max(lb, rb)]++;
      }
  if (flag) return true;
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j)
      if (Map[adj[i][j].first][adj[i][j].second] != -1) {
        if (adj[i][j].first == r || adj[i][j].first == r + 1)
          if (adj[i][j].second == c || adj[i][j].second == c + 1)
            if (i <= adj[i][j].first && j <= adj[i][j].second) continue;
        int lb = Map[i][j], rb = Map[adj[i][j].first][adj[i][j].second];
        exist[min(lb, rb) * 10 + max(lb, rb)]--;
      }
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j) Map[i][j] = -1;
  return false;
}
void reset(int r, int c) {
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j)
      if (Map[adj[i][j].first][adj[i][j].second] != -1) {
        if (adj[i][j].first == r || adj[i][j].first == r + 1)
          if (adj[i][j].second == c || adj[i][j].second == c + 1)
            if (i <= adj[i][j].first && j <= adj[i][j].second) continue;
        int lb = Map[i][j], rb = Map[adj[i][j].first][adj[i][j].second];
        exist[min(lb, rb) * 10 + max(lb, rb)]--;
      }
  for (int i = r; i <= r + 1; ++i)
    for (int j = c; j <= c + 1; ++j) Map[i][j] = -1;
  return;
}
void dfs(int v, int lst) {
  if (v == 7) {
    if (ans == 0) {
      for (int i = 1; i <= (int)(n); ++i)
        for (int j = 1; j <= (int)(m); ++j) ansMap[i][j] = Map[i][j];
    }
    ans++;
  }
  while (vis[lst]) lst++;
  if (!place(pos[lst].first, pos[lst].second, v)) return;
  vis[lst] = true;
  for (int i = lst + 1; i < kp; ++i)
    if (!vis[i]) {
      if (place(pos[i].first, pos[i].second, v)) {
        vis[i] = true;
        dfs(v + 1, lst + 1);
        reset(pos[i].first, pos[i].second);
        vis[i] = false;
      }
    }
  reset(pos[lst].first, pos[lst].second);
  vis[lst] = false;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= (int)(n); ++i)
    for (int j = 1; j <= (int)(m); ++j) scanf(" %c", &ch[i][j]);
  for (int i = 1; i <= (int)(n); ++i)
    for (int j = 1; j <= (int)(m); ++j)
      if (ch[i][j] != '.' && !vis2[i][j]) {
        vis2[i][j] = vis2[i + 1][j] = vis2[i][j + 1] = vis2[i + 1][j + 1] =
            true;
        pos[kp++] = make_pair(i, j);
      }
  for (int i = 1; i <= (int)(n); ++i)
    for (int j = 1; j <= (int)(m); ++j)
      if (ch[i][j] != '.') {
        for (int k = 0; k < (int)(4); ++k) {
          int ni = i + dx[k], nj = j + dy[k];
          if (ch[i][j] == ch[ni][nj]) adj[i][j] = make_pair(ni, nj);
        }
      }
  memset(Map, -1, sizeof(Map));
  dfs(0, 0);
  for (int i = 1; i <= 7; ++i) ans *= i;
  printf("%lld\n", ans);
  for (int i = 1; i <= (int)(n); ++i) {
    for (int j = 1; j <= (int)(m); ++j) {
      if (ansMap[i][j] == -1)
        printf(".");
      else
        printf("%d", ansMap[i][j]);
    }
    printf("\n");
  }
  return 0;
}
