#include <bits/stdc++.h>
using namespace std;
int nextInt() {
  int x;
  scanf("%d", &x);
  return x;
}
string nextString() {
  char buf[100];
  scanf("%s", buf);
  return buf;
}
vector<string> field;
vector<vector<int> > squareId;
vector<vector<pair<int, int> > > domino(28);
vector<string> resField;
bool isValid(vector<int> &squareColor) {
  vector<vector<bool> > used(7, vector<bool>(7));
  for (int i = 0; i < 28; ++i) {
    int x = squareColor[squareId[domino[i][0].first][domino[i][0].second]];
    int y = squareColor[squareId[domino[i][1].first][domino[i][1].second]];
    if (x > y) {
      swap(x, y);
    }
    if (used[x][y]) {
      return false;
    }
    used[x][y] = true;
  }
  return true;
}
int rec(int at, vector<int> &squareColor, vector<int> &colorCount,
        int minUnused) {
  if (at == squareColor.size()) {
    if (isValid(squareColor)) {
      if (resField.empty()) {
        resField = field;
        for (int i = 0; i < resField.size(); ++i) {
          for (int j = 0; j < resField[i].size(); ++j) {
            if (resField[i][j] != '.') {
              resField[i][j] = squareColor[squareId[i][j]] + '0';
            }
          }
        }
      }
      return true;
    }
    return false;
  }
  int res = 0;
  for (int i = 0; i < minUnused; ++i) {
    if (colorCount[i] > 0) {
      --colorCount[i];
      squareColor[at] = i;
      res += rec(at + 1, squareColor, colorCount, minUnused);
      ++colorCount[i];
    }
  }
  if (minUnused < colorCount.size()) {
    --colorCount[minUnused];
    squareColor[at] = minUnused;
    res += rec(at + 1, squareColor, colorCount, minUnused + 1);
    ++colorCount[minUnused];
  }
  return res;
}
int main() {
  int n = nextInt();
  int m = nextInt();
  field = vector<string>(n);
  for (int i = 0; i < n; ++i) {
    field[i] = nextString();
  }
  int squareCount = 0;
  squareId = vector<vector<int> >(n, vector<int>(m, -1));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (field[i][j] != '.' && squareId[i][j] == -1) {
        squareId[i][j] = squareCount;
        squareId[i + 1][j] = squareCount;
        squareId[i][j + 1] = squareCount;
        squareId[i + 1][j + 1] = squareCount;
        ++squareCount;
      }
    }
  }
  domino = vector<vector<pair<int, int> > >(28);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      if (field[i][j] != '.') {
        int dominoId = field[i][j] == 'A'   ? 26
                       : field[i][j] == 'B' ? 27
                                            : field[i][j] - 'a';
        domino[dominoId].push_back(make_pair(i, j));
      }
    }
  }
  vector<int> squareColor(14);
  vector<int> colorCount(7, 2);
  int res = rec(0, squareColor, colorCount, 0);
  for (int i = 2; i <= 7; ++i) {
    res *= i;
  }
  cout << res << endl;
  for (int i = 0; i < resField.size(); ++i) {
    cout << resField[i] << endl;
  }
  return 0;
}
