#include <bits/stdc++.h>
using namespace std;
long long ret = 0;
char buf[40][40];
char sq[40][40];
char ans[40][40];
int n, m;
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
bool was[30];
int color[30];
int get_id(int v1, int v2) {
  int ret = 0, cur = 0;
  while (cur < min(v1, v2)) {
    ret += 7 - cur;
    cur++;
  }
  return ret + max(v1, v2) - cur;
}
void DFS(int x, int y, int mx) {
  if (x == n - 1 && y == m - 1) {
    if (!ret) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
          if (buf[i][j] == '.') {
            ans[i][j] = '.';
          } else {
            ans[i][j] = color[sq[i][j]] + '0';
          }
        }
      }
    }
    ret++;
    return;
  }
  if (buf[x][y] == '.') {
    DFS(x + (y + 1) / m, (y + 1) % m, mx);
  } else {
    for (int i = 0; i < 4; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx > -1 && nx < n && ny > -1 && ny < m && buf[nx][ny] == buf[x][y]) {
        if (color[sq[x][y]] != -1) {
          if (i > 1) {
            if (was[get_id(color[sq[x][y]], color[sq[nx][ny]])]) {
              return;
            } else {
              was[get_id(color[sq[x][y]], color[sq[nx][ny]])] = true;
              DFS(x + (y + 1) / m, (y + 1) % m, mx);
              was[get_id(color[sq[x][y]], color[sq[nx][ny]])] = false;
            }
          } else {
            DFS(x + (y + 1) / m, (y + 1) % m, mx);
          }
        } else {
          for (int k = 0; k <= min(mx, 6); k++) {
            if (i > 1) {
              if (was[get_id(color[sq[nx][ny]], k)]) {
                continue;
              } else {
                color[sq[x][y]] = k;
                was[get_id(color[sq[x][y]], color[sq[nx][ny]])] = true;
                DFS(x + (y + 1) / m, (y + 1) % m, max(mx, k + 1));
                was[get_id(color[sq[x][y]], color[sq[nx][ny]])] = false;
                color[sq[x][y]] = -1;
              }
            } else {
              color[sq[x][y]] = k;
              DFS(x + (y + 1) / m, (y + 1) % m, max(mx, k + 1));
              color[sq[x][y]] = -1;
            }
          }
        }
      }
    }
  }
}
int main() {
  memset(sq, -1, sizeof sq);
  memset(color, -1, sizeof color);
  int kol = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> buf[i];
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (buf[i][j] != '.' && sq[i][j] == -1) {
        sq[i][j] = sq[i + 1][j] = sq[i][j + 1] = sq[i + 1][j + 1] = kol++;
      }
    }
  }
  DFS(0, 0, 0);
  cout << ret * 5040 << endl;
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }
}
