#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vecll = vector<ll>;
constexpr int INF = 2000000100;
constexpr ll DIV = 1000000007;
template <class T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T>
T divup(T a, T b) {
  if (a % b == 0) {
    return a / b;
  }
  return a / b + 1;
}
std::string strMulti(std::string t, int n) {
  std::string out = "";
  for (int i = 0; i < n; i++) {
    out += t;
  }
  return out;
}
template <class T>
T math_P(T m, T n) {
  T ret = 1;
  for (T i = m; i > m - n; i--) {
    ret *= i;
  }
  return ret;
}
template <class T>
T math_C(T m, T n) {
  T ret = mathP(m, n);
  for (T i = 2; i <= n; i++) {
    ret /= i;
  }
  return ret;
}
template <class T>
bool compare_by_b(pair<T, T> a, pair<T, T> b) {
  if (a.second != b.second) {
    return a.second < b.second;
  }
  return a.first < b.first;
}
template <class T>
T math_gcd(T a, T b) {
  a = abs(a);
  b = abs(b);
  if (a < b) {
    std::swap(a, b);
  }
  T tmp;
  while (b != 0) {
    tmp = b;
    b = a % b;
    a = tmp;
  }
  return a;
}
template <class T>
T math_lcm(T a, T b) {
  a = abs(a);
  b = abs(b);
  if (a > b) {
    std::swap(a, b);
  }
  if (a == 1) {
    return b;
  }
  T i = a;
  while (i % b != 0) {
    i += a;
  }
  return i;
}
void Main() {
  int H, W;
  cin >> H >> W;
  vector<vector<int>> S(W, vector<int>(H));
  for (int y = 0, y_len = (H); y < y_len; y++)
    for (int x = 0, x_len = (W); x < x_len; x++) {
      char c;
      cin >> c;
      if (c == '.') {
        S[x][y] = 0;
      } else
        S[x][y] = 1;
    }
  vector<vector<vector<pair<int, int>>>> Link(
      W, vector<vector<pair<int, int>>>(H));
  for (int x = 0, x_len = (W); x < x_len; x++)
    for (int y = 0, y_len = (H); y < y_len; y++) {
      if (S[x][y] == 1) continue;
      int num = 0;
      if (x - 1 >= 0 && S[x - 1][y] == 0) num++;
      if (y - 1 >= 0 && S[x][y - 1] == 0) num++;
      if (x + 1 < W && S[x + 1][y] == 0) num++;
      if (y + 1 < H && S[x][y + 1] == 0) num++;
      Link[x][y].resize(num);
      int i = 0;
      if (x - 1 >= 0 && S[x - 1][y] == 0) {
        Link[x][y][i] = make_pair(x - 1, y);
        i++;
      }
      if (y - 1 >= 0 && S[x][y - 1] == 0) {
        Link[x][y][i] = make_pair(x, y - 1);
        i++;
      }
      if (x + 1 < W && S[x + 1][y] == 0) {
        Link[x][y][i] = make_pair(x + 1, y);
        i++;
      }
      if (y + 1 < H && S[x][y + 1] == 0) {
        Link[x][y][i] = make_pair(x, y + 1);
        i++;
      }
    }
  int ret = -1;
  for (int x = 0, x_len = (W); x < x_len; x++)
    for (int y = 0, y_len = (H); y < y_len; y++) {
      if (S[x][y] == 1) continue;
      (std::cerr << strMulti("\t", 0) << "x"
                 << " : " << (x) << "\n");
      (std::cerr << strMulti("\t", 1) << "y"
                 << " : " << (y) << "\n");
      queue<pair<int, int>> que;
      vector<vector<int>> map(W, vector<int>(H, INF));
      que.push(make_pair(x, y));
      for (int mx = 0, mx_len = (W); mx < mx_len; mx++) {
        for (int my = 0, my_len = (H); my < my_len; my++) {
          cout << map[x][y];
        }
        cout << endl;
      }
      map[x][y] = 0;
      for (int mx = 0, mx_len = (W); mx < mx_len; mx++) {
        for (int my = 0, my_len = (H); my < my_len; my++) {
          cout << map[x][y];
        }
        cout << endl;
      }
      int max = -1;
      while (!que.empty()) {
        pair<int, int> pos = que.front();
        int num = map[x][y];
        chmax(max, num);
        for (int i = 0, i_len = (Link[x][y].size()); i < i_len; i++) {
          for (int mx = 0, mx_len = (W); mx < mx_len; mx++) {
            for (int my = 0, my_len = (H); my < my_len; my++) {
              cout << map[x][y];
            }
            cout << endl;
          }
          pair<int, int> next = Link[x][y][i];
          if (num + 1 < map[next.first][next.second]) {
            map[next.first][next.second] = num + 1;
            que.push(next);
          }
        }
        que.pop();
      }
      chmax(ret, max);
      for (int mx = 0, mx_len = (W); mx < mx_len; mx++) {
        for (int my = 0, my_len = (H); my < my_len; my++) {
          cout << map[x][y];
        }
        cout << endl;
      }
    }
  cout << ret;
}
int main() {
  Main();
  return 0;
}
