//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
 
#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>
#include<iomanip>
 
#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define pii pair<int,int>
#define pli pair<ll,int>
#define pll pair<ll,ll>
#define mp(a, b) make_pair(a, b)
#define mt(a,b,c) make_tuple(a,b,c)
#define rank qwertyuio
#define next dfghjk
#define plus fsghsf
#define minus ytryr
#define pb push_back
using namespace std;
 
inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }
 
const int N = 100000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int mod = (int)1e9 + 7;
const int mod1 = (int)1e9 + 9;
const int mod2 = 998244353;
const int mod3 = (int)1e6+3;
const ld eps = 1e-12;
const double pi = atan2(0, -1);
const int ABC = 26;
#define ent '\n'
ll fac[100007]; 
 
bool sortbysec(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.second < b.second); 
} 
bool sortbyfirst(const pair<int,int> &a, 
              const pair<int,int> &b) 
{ 
    return (a.first > b.first); 
} 
 
ll power(ll x, ll y) 
{ 
    ll res = 1;      // Initialize result 
  
    x = x % mod;  // Update x if it is more than or 
                // equal to p 
  
    while (y > 0) 
    { 
        // If y is odd, multiply x with result 
        if (y & 1) 
            res = (res*x) % mod; 
  
        // y must be even now 
        y = y>>1; // y = y/2 
        x = (x*x) % mod; 
    } 
    return res; 
} 
ll modInverse(ll n) 
{ 
    return power(n, mod-2); 
} 
  
 
ll nCrModPFermat(ll n, ll r) 
{ 
   // Base case 
   if (r==0) 
      return 1; 
  
    return (fac[n]* modInverse(fac[r]) % mod * 
            modInverse(fac[n-r]) % mod) % mod; 
} 
//int saved[1000000007];
ll binarySearch(vector<ll> arr, ll l, ll r, ll x) 
{ 
    if (r>=l) 
    { 
        ll mid = l + (r - l)/2; 
        if (arr[mid] == x) 
            return mid; 
        if (arr[mid] > x) 
            return binarySearch(arr, l, mid-1, x); 
        return binarySearch(arr, mid+1, r, x); 
    } 
    return -1; 
} 
ll findPos(vector<ll> arr, ll key) 
{ 
    ll l = 0, h = 1; 
    ll val = arr[0]; 
  
    // Find h to do binary search 
    while (val < key) 
    { 
        l = h;        // store previous high 
        h = 2*h;      // double high index 
        val = arr[h]; // update new val 
    } 
 
    return binarySearch(arr, l, h, key); 
} 
int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);<<" "
    /*int t;
    cin>>t;
    while(t--){
        
        ll x;
        cin>>x;
        if(x<=14){
            cout<<"NO"<<endl;
            continue;
        }
        if(x%14>=1&&x%14<=6){
            cout<<"YES"<<endl;
        }
        else{
            cout<<"NO"<<endl;
        }
        
    }*/
    int h,w;
    cin>>h>>w;
    char a[h][w];
    int i,j;
    //int count=0;
    int ope=0;
    queue<pii> que;
    int black=0;
    rep(i,0,h){
        rep(j,0,w){
            cin>>a[i][j];
            if(a[i][j]=='#'){
                black++;
                que.push(mp(i,j));
            }
        }
    }
    int count=que.size();
    
    while(que.size()!=0){
        //cout<<que.front().first<<" "<<que.front().second<<endl;
        //cout<<que.back().first<<" "<<que.back().second<<endl;
        i=que.front().first;
        j=que.front().second;
        
        if(i<h-1&&a[i+1][j]!='#'){
            
            a[i+1][j]='#';
            que.push(mp(i+1,j));
        }
        if(j<w-1&&a[i][j+1]!='#'){
            
            a[i][j+1]='#';
            que.push(mp(i,j+1));
        }
        if(i>=1&&a[i-1][j]!='#'){
            
            a[i-1][j]='#';
            que.push(mp(i-1,j));
        }
        if(j>=1&&a[i][j-1]!='#'){
            
            a[i][j-1]='#';
            que.push(mp(i,j-1));
        }
        
        que.pop();
        //cout<<que.front().first<<" "<<que.front().second<<endl;
       // cout<<que.back().first<<" "<<que.back().second<<endl;
        //cout<<endl;
        count--;
        /*rep(i,0,h){
                rep(j,0,w){
                    cout<<a[i][j];
                }       
                cout<<endl;
            }
            cout<<endl;*/
        if(count==0){
            //cout<<"YEY"<<endl;
        

            count=que.size();
            ope++;
        }
        
    }
    
    cout<<ope-1;
    //cout<<max(max(tl,tr),max(bl,br));
    

    return 0;
             
    
}

Measured memory_bytes: 9360