#include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>
#include <set>
#include <limits>
#include <iomanip>
#include <stack>


using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;
#define upd_min(miner, updater) if (miner > updater) miner = updater;

void ansYes(bool flag) {
    if (flag) cout << "Yes" << endl; else cout << "No" << endl;
}

void ansYES(bool flag) {
    if (flag) cout << "YES" << endl; else cout << "NO" << endl;
}

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}

class Graph {
protected:
    int n;
    vector<Int> prev;
    vector<Int> minD;
    vector<vector<node>> ker;
public:
    Graph(Int N) : n(N), prev(N), minD(N), ker(N) {}

    void addEdge(Int from, Int next, Int cost) {
        ker.at(from).push_back({next, cost});
        ker.at(next).push_back({from, cost});
    }

    void addDirectedEdge(Int from, Int next, Int cost) {
        ker.at(from).push_back({next, cost});
    }

    void dijkstra(Int s) {
        REP(n, i) {
            minD.at(i) = INF;
        }
        minD.at(s) = 0;
        priority_queue<pll, vector<pll >, greater<>> que;
        que.push({0, s});
        while (!que.empty()) {
            auto z = que.top();
            que.pop();
            int v = z.second;
            if (minD[v] < z.first) continue;
            REP(ker.at(v).size(), i) {
                node n = ker.at(v).at(i);
                if (minD[n.to] > minD[v] + n.length) {
                    minD[n.to] = minD[v] + n.length;
                    que.push({minD[n.to], n.to});
                }
            }
        }
    }

    bool bellmanford(Int s) {
        bool flag = false;
        REP(n, i) {
            minD.at(i) = INF;
        }
        minD.at(s) = 0;
        REP(n - 1, i) {
            REP(n, k) {
                for (auto z: ker.at(k)) {
                    if (minD[k] != INF && minD[z.to] > minD[k] + z.length) {
                        minD[z.to] = minD[k] + z.length;
                    }
                }
            }
        }

        REP(n, k) {
            for (auto z: ker.at(k)) {
                if (minD[k] != INF && minD[z.to] > minD[k] + z.length) {
                    minD[z.to] = minD[k] + z.length;
                    flag = true;
                }
            }
        }
        return flag;
    }

    Int getD(Int x) {
        return minD[x];
    }

    Int putD(Int x, string sep = "\n") {
        if (minD[x] == INF) {
            cout << "INF" << sep;
        } else {
            cout << minD[x] << sep;
        }
        return minD[x];
    }
};


/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

int dx[4] = {0, -1, 0, 1};
int dy[4] = {-1, 0, 1, 0};

struct datas {
    Int max;
    Int size;
};

class ContainChecker {
protected:
    Int minx, miny, maxx, maxy;
public:
    ContainChecker(Int minx, Int miny, Int maxx, Int maxy) : minx(minx), miny(miny), maxx(maxx), maxy(maxy) {};

    bool in(Int x, Int y) {
        return (minx <= x && miny <= y && x <= maxx && y <= maxy);
    }
};

Int getHead(Int n) {
    while (n / 10 > 0) {
        n /= 10;
    }
    n %= 10;
    return n;
}


int main(void) {
    Int h, w;
    cin >> h >> w;
    char m[1001][1001];
    REP(h, y) {
    REP(w, x) {
            cin >> m[x][y];
        }
    }
    ContainChecker cc = ContainChecker(0, 0, w, h);
    queue<pll > que;
    Int color = 0;
    REP(w, x) {
        REP(h, y) {
            if (m[x][y] == '#') que.push({x, y}); else color++;
        }
    }
    que.push({-1,-1});
    Int cnt = 0;
    bool flag = false;
    while (!que.empty()) {
        Int x = que.front().first;
        Int y = que.front().second;
        que.pop();
        if (x == -1) {
            if (flag) break;
            cnt++;
            /*REP(h,y) {
                REP(w, x) {
                    cout << m[x][y];
                }
                cout << endl;
            }*/
            flag = true;
            que.push({-1,-1});
            continue;
        }
        flag = false;
        REP(4, i) {
            Int xx = x + dx[i];
            Int yy = y + dy[i];
            if (cc.in(xx, yy) && m[xx][yy] == '.') {
                m[xx][yy] = '#';
                que.push({xx,yy});
                color--;
            }
        }


    }
    cout << cnt - 1 << endl;
}




Measured memory_bytes: 17588