#include <bits/stdc++.h>
using namespace std;
int N, M, C, ans;
char a[35][35];
pair<int, int> oth[35][35];
int id[35][35];
int col[20], solcol[20];
int use[10][10], newuse[10][10];
vector<pair<int, int> > cmp[20];
map<char, vector<pair<int, int> > > mp;
void uncolor(int idd) {
  for (auto pr : cmp[idd]) {
    int a1, b1, a2, b2;
    tie(a1, b1) = pr;
    tie(a2, b2) = oth[a1][b1];
    if (id[a2][b2] <= idd) {
      int x = col[idd];
      int y = col[id[a2][b2]];
      if (x > y) swap(x, y);
      use[x][y] = 0;
    }
  }
  col[idd] = -1;
}
bool trycolor(int idd, int cl) {
  col[idd] = cl;
  for (int i = 0; i <= 6; i++)
    for (int j = 0; j <= 6; j++) newuse[i][j] = use[i][j];
  for (auto pr : cmp[idd]) {
    int a1, b1, a2, b2;
    tie(a1, b1) = pr;
    tie(a2, b2) = oth[a1][b1];
    if (id[a2][b2] > idd) continue;
    if (id[a2][b2] == idd) {
      if (a1 > a2 || b1 > b2) continue;
      if (newuse[cl][cl]) return false;
      newuse[cl][cl] = 1;
      continue;
    }
    int c2 = col[id[a2][b2]];
    int c1 = cl;
    if (c1 > c2) swap(c1, c2);
    if (newuse[c1][c2]) return false;
    newuse[c1][c2] = 1;
  }
  for (int i = 0; i <= 6; i++)
    for (int j = 0; j <= 6; j++) use[i][j] = newuse[i][j];
  return true;
}
void bck(int idd, int cl) {
  if (idd > C) {
    ans++;
    for (int i = 1; i <= C; i++) solcol[i] = col[i];
    return;
  }
  for (int c = 0; c <= cl; c++)
    if (trycolor(idd, c)) {
      bck(idd + 1, cl);
      uncolor(idd);
    }
  if (cl < 6) {
    if (trycolor(idd, cl + 1)) {
      bck(idd + 1, cl + 1);
      uncolor(idd);
    }
  }
}
int main() {
  scanf("%d%d\n", &N, &M);
  for (int i = 1; i <= N; i++) scanf("%s\n", a[i] + 1);
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      if (a[i][j] != '.') mp[a[i][j]].push_back({i, j});
  for (auto pr : mp) {
    auto vec = pr.second;
    assert(vec.size() == 2);
    int a1, a2, b1, b2;
    tie(a1, b1) = vec[0];
    tie(a2, b2) = vec[1];
    oth[a1][b1] = {a2, b2};
    oth[a2][b2] = {a1, b1};
  }
  C = 14;
  for (int c = 1; c <= C; c++) {
    int x = -1, y = -1;
    for (int i = 1; i <= N && x == -1; i++)
      for (int j = 1; j <= M; j++)
        if (id[i][j] == 0 && a[i][j] != '.') {
          x = i, y = j;
          break;
        }
    assert(x != -1);
    id[x][y] = c;
    cmp[c].emplace_back(x, y);
    x++;
    id[x][y] = c;
    cmp[c].emplace_back(x, y);
    y++;
    id[x][y] = c;
    cmp[c].emplace_back(x, y);
    x--;
    id[x][y] = c;
    cmp[c].emplace_back(x, y);
    y--;
    sort(cmp[c].begin(), cmp[c].end());
  }
  for (int i = 1; i <= N; i++)
    for (int j = 1; j <= M; j++)
      if (a[i][j] != '.') assert(id[i][j] != 0);
  bck(1, -1);
  ans *= 5040;
  printf("%d\n", ans);
  for (int i = 1; i <= N; i++, printf("\n"))
    for (int j = 1; j <= M; j++) {
      if (a[i][j] == '.')
        printf(".");
      else
        printf("%d", solcol[id[i][j]]);
    }
  return 0;
}
