stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <queue>

#include <cstring>

using namespace std;



//char s[1003][1003];

int vis[1003][1003];

struct node

{

    int x;int y;

};



int main()

{

    int h,w;

    cin>>h>>w;

    queue<node> q;

    memset(vis,0,sizeof(vis));

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            char s;cin>>s;

            if(s=='#'){

                node a;a.x=i;a.y=j;

                q.push(a);

                vis[i][j]=1;

            }

        }

    }

    while(!q.empty()){

        node a;a=q.front();q.pop();

        if(a.x>0&&!vis[a.x-1][a.y]){

            vis[a.x-1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x-1;b.y=a.y;

            q.push(b);

        }

        if(a.y>0&&!vis[a.x][a.y-1]){

            vis[a.x][a.y-1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y-1;

            q.push(b);

        }

        if(a.x<h-1&&!vis[a.x+1][a.y]){

            vis[a.x+1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x+1;b.y=a.y;

            q.push(b);

        }

        if(a.y<w-1&&!vis[a.x][a.y+1]){

            vis[a.x][a.y+1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y+1;

            q.push(b);

        }

    }

    int ans=0;

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            ans=max(ans,vis[i][j]);

            //cout<<vis[i][j];

        }

        //cout<<endl;

    }

    cout<<ans-1<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.011090602741307x):
#include <bits/stdc++.h>

using namespace std;

int arr[1005][1005];

queue <pair<int,int> > q;

int langkah[1005][1005];

int main(){

	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int x,y; cin >> x >> y;

	char c;

	for(int i=1;i<=x;i++)

		for(int j=1;j<=y;j++){

			cin >> c;

			langkah[i][j]=-1;

			if(c=='#'){

				arr[i][j]=1;

				q.push({i,j});

				langkah[i][j]=0;

			}

			else arr[i][j]=0;

		}

		while(!q.empty()){

			pair<int,int> qq;

			qq=q.front();

			int a = qq.first;

			int b = qq.second;

			q.pop();

			//cout << a <<' '<<b <<' '<<langkah[a][b]<<" LOL "<<endl;

			if(b<y && langkah[a][b+1]==-1){

				q.push({a,b+1}); 

				langkah[a][b+1]=langkah[a][b]+1;

			}

			if(b>1 && langkah[a][b-1]==-1){

				q.push({a,b-1});

				langkah[a][b-1]=langkah[a][b]+1;

			}

			if(a<x && langkah[a+1][b]==-1){

				q.push({a+1,b});

				langkah[a+1][b]=langkah[a][b]+1;

			}

			if(a>1 && langkah[a-1][b]==-1){

				q.push({a-1,b});

				langkah[a-1][b]=langkah[a][b]+1;

			}

		}

		int maxi = -100000;

		for(int i=1;i<=x;i++)

			for(int j=1;j<=y;j++){

				maxi=max(maxi,langkah[i][j]);

			}

		cout << maxi << endl;

}
// Slower C++ solution:
#include <iostream>

#include <queue>

#include <cstring>

const int maxn=1010;

using namespace std;

struct Node {

	int x,y;

};

queue <Node> Q;

int a[maxn][maxn],d[maxn][maxn];

int vis[maxn][maxn];

int main(){

	memset(vis,0,sizeof(vis));

	memset(d,0,sizeof(d));

	ios::sync_with_stdio(false);

	cin.tie(0);

	int h,w;

	cin >> h>>w;

	for(int i=1;i<=h;i++){

		string s;

		cin >> s;

		for (int j=0;j<w;j++) {

			if (s[j] == '#'){

				a[i][j+1] = 1;

				Q.push((Node){i,j+1});

				vis[i][j+1] = 1;

			}else a[i][j+1] = 0;

			// a[i][j+1] = s[j]=='h'?1:0;



		}

	}



	int cnt = 0;

	while (!Q.empty()){

		Node p=Q.front();

		Q.pop();

		if (p.x>1 && !vis[p.x-1][p.y]){

			d[p.x-1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x-1,p.y});

			vis[p.x-1][p.y] = 1;

		}

		if (p.y>1 && !vis[p.x][p.y-1]){

			d[p.x][p.y-1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y-1});

			vis[p.x][p.y-1] = 1;

		}

		if (p.x<h && !vis[p.x+1][p.y]){

			d[p.x+1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x+1,p.y});

			vis[p.x+1][p.y] = 1;

		}

		if (p.y<w && !vis[p.x][p.y+1]){

			d[p.x][p.y+1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y+1});

			vis[p.x][p.y+1] = 1;

		}

	}

	int ans=0;

	// for (int i=1;i<=h;i++){

	// 	for (int j=1;j<=w;j++) ans=max(ans,d[i][j]),cout << d[i][j]<<' ';

	// 	cout << endl;

	// }

	for (int i=1;i<=h;i++)

		for (int j=1;j<=w;j++) ans=max(ans,d[i][j]);

	

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 9.393163304182726x):
#include <bits/stdc++.h>

using namespace std;

int arr[1005][1005];

queue <pair<int,int> > q;

int langkah[1005][1005];

int main(){

	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	int x,y; cin >> x >> y;

	char c;

	for(int i=1;i<=x;i++)

		for(int j=1;j<=y;j++){

			cin >> c;

			langkah[i][j]=-1;

			if(c=='#'){

				arr[i][j]=1;

				q.push({i,j});

				langkah[i][j]=0;

			}

			else arr[i][j]=0;

		}

		while(!q.empty()){

			pair<int,int> qq;

			qq=q.front();

			int a = qq.first;

			int b = qq.second;

			q.pop();

			//cout << a <<' '<<b <<' '<<langkah[a][b]<<" LOL "<<endl;

			if(b<y && langkah[a][b+1]==-1){

				q.push({a,b+1}); 

				langkah[a][b+1]=langkah[a][b]+1;

			}

			if(b>1 && langkah[a][b-1]==-1){

				q.push({a,b-1});

				langkah[a][b-1]=langkah[a][b]+1;

			}

			if(a<x && langkah[a+1][b]==-1){

				q.push({a+1,b});

				langkah[a+1][b]=langkah[a][b]+1;

			}

			if(a>1 && langkah[a-1][b]==-1){

				q.push({a-1,b});

				langkah[a-1][b]=langkah[a][b]+1;

			}

		}

		int maxi = -100000;

		for(int i=1;i<=x;i++)

			for(int j=1;j<=y;j++){

				maxi=max(maxi,langkah[i][j]);

			}

		cout << maxi << endl;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <queue>

#include <cstring>

using namespace std;



//char s[1003][1003];

int vis[1003][1003];

struct node

{

    int x;int y;

};



int main()

{

    int h,w;

    cin>>h>>w;

    queue<node> q;

    memset(vis,0,sizeof(vis));

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            char s;cin>>s;

            if(s=='#'){

                node a;a.x=i;a.y=j;

                q.push(a);

                vis[i][j]=1;

            }

        }

    }

    while(!q.empty()){

        node a;a=q.front();q.pop();

        if(a.x>0&&!vis[a.x-1][a.y]){

            vis[a.x-1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x-1;b.y=a.y;

            q.push(b);

        }

        if(a.y>0&&!vis[a.x][a.y-1]){

            vis[a.x][a.y-1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y-1;

            q.push(b);

        }

        if(a.x<h-1&&!vis[a.x+1][a.y]){

            vis[a.x+1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x+1;b.y=a.y;

            q.push(b);

        }

        if(a.y<w-1&&!vis[a.x][a.y+1]){

            vis[a.x][a.y+1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y+1;

            q.push(b);

        }

    }

    int ans=0;

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            ans=max(ans,vis[i][j]);

            //cout<<vis[i][j];

        }

        //cout<<endl;

    }

    cout<<ans-1<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 2.50982031427459x):
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

typedef pair<int,pii> piii;

int r,c,dr[]={0,0,-1,1},dc[]={-1,1,0,0};

vector<pii> black;

const int MAXN=1005;

bool visited[MAXN][MAXN];

char a[MAXN][MAXN];



bool border(int x,int y){

	return x>=1 && x<=r && y>=1 && y<=c;

}



int main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin >> r >> c;

	for(int i=1;i<=r;i++){

		for(int j=1;j<=c;j++){

			cin >> a[i][j];

			if(a[i][j]=='#')black.push_back({i,j});

		}

	}

	queue<piii> Q;

	memset(visited,0,sizeof(visited));

	for(auto isi : black){

		Q.push({0,{isi.first,isi.second}});

		visited[isi.first][isi.second]=true;

	}

	int ans=-1;

	while(!Q.empty()){

		piii now=Q.front();

		ans=max(ans,now.first);

		Q.pop();

		for(int i=0;i<4;i++){

			int xnow=now.second.first+dr[i],ynow=now.second.second+dc[i];

			if(!visited[xnow][ynow] && border(xnow,ynow) && a[xnow][ynow]=='.'){

				visited[xnow][ynow]=true;

				Q.push({now.first+1,{xnow,ynow}});

			}

		}

	}

	cout << ans << endl;

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <queue>

#include <cstring>

const int maxn=1010;

using namespace std;

struct Node {

	int x,y;

};

queue <Node> Q;

int a[maxn][maxn],d[maxn][maxn];

int vis[maxn][maxn];

int main(){

	memset(vis,0,sizeof(vis));

	memset(d,0,sizeof(d));

	ios::sync_with_stdio(false);

	cin.tie(0);

	int h,w;

	cin >> h>>w;

	for(int i=1;i<=h;i++){

		string s;

		cin >> s;

		for (int j=0;j<w;j++) {

			if (s[j] == '#'){

				a[i][j+1] = 1;

				Q.push((Node){i,j+1});

				vis[i][j+1] = 1;

			}else a[i][j+1] = 0;

			// a[i][j+1] = s[j]=='h'?1:0;



		}

	}



	int cnt = 0;

	while (!Q.empty()){

		Node p=Q.front();

		Q.pop();

		if (p.x>1 && !vis[p.x-1][p.y]){

			d[p.x-1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x-1,p.y});

			vis[p.x-1][p.y] = 1;

		}

		if (p.y>1 && !vis[p.x][p.y-1]){

			d[p.x][p.y-1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y-1});

			vis[p.x][p.y-1] = 1;

		}

		if (p.x<h && !vis[p.x+1][p.y]){

			d[p.x+1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x+1,p.y});

			vis[p.x+1][p.y] = 1;

		}

		if (p.y<w && !vis[p.x][p.y+1]){

			d[p.x][p.y+1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y+1});

			vis[p.x][p.y+1] = 1;

		}

	}

	int ans=0;

	// for (int i=1;i<=h;i++){

	// 	for (int j=1;j<=w;j++) ans=max(ans,d[i][j]),cout << d[i][j]<<' ';

	// 	cout << endl;

	// }

	for (int i=1;i<=h;i++)

		for (int j=1;j<=w;j++) ans=max(ans,d[i][j]);

	

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 4.704595056261745x):
#include<bits/stdc++.h>

using namespace std;

typedef pair<int,int> pii;

typedef pair<int,pii> piii;

int r,c,dr[]={0,0,-1,1},dc[]={-1,1,0,0};

vector<pii> black;

const int MAXN=1005;

bool visited[MAXN][MAXN];

char a[MAXN][MAXN];



bool border(int x,int y){

	return x>=1 && x<=r && y>=1 && y<=c;

}



int main(){

	ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

	cin >> r >> c;

	for(int i=1;i<=r;i++){

		for(int j=1;j<=c;j++){

			cin >> a[i][j];

			if(a[i][j]=='#')black.push_back({i,j});

		}

	}

	queue<piii> Q;

	memset(visited,0,sizeof(visited));

	for(auto isi : black){

		Q.push({0,{isi.first,isi.second}});

		visited[isi.first][isi.second]=true;

	}

	int ans=-1;

	while(!Q.empty()){

		piii now=Q.front();

		ans=max(ans,now.first);

		Q.pop();

		for(int i=0;i<4;i++){

			int xnow=now.second.first+dr[i],ynow=now.second.second+dc[i];

			if(!visited[xnow][ynow] && border(xnow,ynow) && a[xnow][ynow]=='.'){

				visited[xnow][ynow]=true;

				Q.push({now.first+1,{xnow,ynow}});

			}

		}

	}

	cout << ans << endl;

	return 0;

}
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <queue>

#include <cstring>

using namespace std;



//char s[1003][1003];

int vis[1003][1003];

struct node

{

    int x;int y;

};



int main()

{

    int h,w;

    cin>>h>>w;

    queue<node> q;

    memset(vis,0,sizeof(vis));

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            char s;cin>>s;

            if(s=='#'){

                node a;a.x=i;a.y=j;

                q.push(a);

                vis[i][j]=1;

            }

        }

    }

    while(!q.empty()){

        node a;a=q.front();q.pop();

        if(a.x>0&&!vis[a.x-1][a.y]){

            vis[a.x-1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x-1;b.y=a.y;

            q.push(b);

        }

        if(a.y>0&&!vis[a.x][a.y-1]){

            vis[a.x][a.y-1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y-1;

            q.push(b);

        }

        if(a.x<h-1&&!vis[a.x+1][a.y]){

            vis[a.x+1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x+1;b.y=a.y;

            q.push(b);

        }

        if(a.y<w-1&&!vis[a.x][a.y+1]){

            vis[a.x][a.y+1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y+1;

            q.push(b);

        }

    }

    int ans=0;

    for(int i=0;i<h;i++){

        for(int j=0;j<w;j++){

            ans=max(ans,vis[i][j]);

            //cout<<vis[i][j];

        }

        //cout<<endl;

    }

    cout<<ans-1<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.523294357167201x):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		scanf("%s",c[i]+1);

	queue<int>q;

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			if(c[i][j]=='#'){

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

		for(int i=0;i<4;i++){

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

	printf("%d\n",ans-1);

}
// Slower C++ solution:
#include <iostream>

#include <queue>

#include <cstring>

const int maxn=1010;

using namespace std;

struct Node {

	int x,y;

};

queue <Node> Q;

int a[maxn][maxn],d[maxn][maxn];

int vis[maxn][maxn];

int main(){

	memset(vis,0,sizeof(vis));

	memset(d,0,sizeof(d));

	ios::sync_with_stdio(false);

	cin.tie(0);

	int h,w;

	cin >> h>>w;

	for(int i=1;i<=h;i++){

		string s;

		cin >> s;

		for (int j=0;j<w;j++) {

			if (s[j] == '#'){

				a[i][j+1] = 1;

				Q.push((Node){i,j+1});

				vis[i][j+1] = 1;

			}else a[i][j+1] = 0;

			// a[i][j+1] = s[j]=='h'?1:0;



		}

	}



	int cnt = 0;

	while (!Q.empty()){

		Node p=Q.front();

		Q.pop();

		if (p.x>1 && !vis[p.x-1][p.y]){

			d[p.x-1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x-1,p.y});

			vis[p.x-1][p.y] = 1;

		}

		if (p.y>1 && !vis[p.x][p.y-1]){

			d[p.x][p.y-1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y-1});

			vis[p.x][p.y-1] = 1;

		}

		if (p.x<h && !vis[p.x+1][p.y]){

			d[p.x+1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x+1,p.y});

			vis[p.x+1][p.y] = 1;

		}

		if (p.y<w && !vis[p.x][p.y+1]){

			d[p.x][p.y+1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y+1});

			vis[p.x][p.y+1] = 1;

		}

	}

	int ans=0;

	// for (int i=1;i<=h;i++){

	// 	for (int j=1;j<=w;j++) ans=max(ans,d[i][j]),cout << d[i][j]<<' ';

	// 	cout << endl;

	// }

	for (int i=1;i<=h;i++)

		for (int j=1;j<=w;j++) ans=max(ans,d[i][j]);

	

	cout << ans;

}

// Faster C++ solution for the same problem (measured speedup: 10.353276359753098x):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		scanf("%s",c[i]+1);

	queue<int>q;

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			if(c[i][j]=='#'){

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

		for(int i=0;i<4;i++){

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

	printf("%d\n",ans-1);

}
// Slower C++ solution:
#include <iostream>

#include <string>

#include <queue>



using namespace std ;



struct Node

{

    int x , y ;

    bool black ;

} matrix [ 1000 ] [ 1000 ] ;



string arr [ 1000 ] ;



int main()

{

    int h , w , cnt = 0 , res = 0 ;

    cin >> h >> w ;

    for ( int i = 0 ; i < h ; i ++ )

        cin >> arr [ i ] ;

    queue<Node> q ;

    while ( !q.empty() )

        q.pop() ;

    for ( int i = 0 ; i < h ; i ++ )

    {

        for ( int j = 0 ; j < w ; j ++ )

        {

            matrix [ i ] [ j ].x = i ;

            matrix [ i ] [ j ].y = j ;

            matrix [ i ] [ j ].black = ( arr [ i ] [ j ] == '#' ) ;

            if ( matrix [ i ] [ j ].black )

            {

                q.push ( matrix [ i ] [ j ] ) ;

            }

        }

    }

    Node n ;

    while ( !q.empty() )

    {

        cnt = q.size() ;

        while ( cnt -- )

        {

            n = q.front() ;

            q.pop() ;

            if ( n.x > 0 )

            {

                if ( !matrix [ n.x - 1 ][ n.y ].black )

                {

                    matrix [ n.x - 1 ] [ n.y ].black = true ;

                    q.push ( matrix [ n.x - 1 ] [ n.y ] ) ;

                }

            }

            if ( n.y < w - 1 )

            {

                if ( !matrix [ n.x ] [ n.y + 1 ].black )

                {

                    matrix [ n.x ] [ n.y + 1 ].black = true ;

                    q.push ( matrix [ n.x ] [ n.y + 1 ] ) ;

                }

            }

            if ( n.x < h - 1 )

            {

                if ( !matrix [ n.x + 1 ] [ n.y ].black )

                {

                    matrix [ n.x + 1 ] [ n.y ].black = true ;

                    q.push ( matrix [ n.x + 1 ] [ n.y ] ) ;

                }

            }

            if ( n.y > 0 )

            {

                if ( !matrix [ n.x ] [ n.y - 1 ].black )

                {

                    matrix [ n.x ] [ n.y - 1 ].black = true ;

                    q.push ( matrix [ n.x ] [ n.y - 1 ] ) ;

                }

            }

        }

        res ++ ;

    }

    cout << res - 1 << endl ;

}


// Faster C++ solution for the same problem (measured speedup: 1.1249710086050424x):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		scanf("%s",c[i]+1);

	queue<int>q;

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			if(c[i][j]=='#'){

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

		for(int i=0;i<4;i++){

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

	printf("%d\n",ans-1);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;

int n,m,a[1005][1005],x,y,curx,cury,maks=0;

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

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

char c;

queue<pair<int,int> > q;

int main()

{

	ios_base::sync_with_stdio(false);

	cin.tie(NULL); cout.tie(NULL);

	cin>>n>>m;

	for(int i=1;i<=n;i++){

		for(int j=1;j<=m;j++){

			cin>>c;

			if(c=='#'){

				a[i][j]=0;

				q.push(make_pair(i,j));

			}

			else{

				a[i][j]=-1;

			}

		}

	}

	while(!q.empty()){

		x=q.front().first;

		y=q.front().second;

		q.pop();

		for(int i=0;i<4;i++){

			curx=x+gx[i];cury=y+gy[i];

			if(a[curx][cury]==-1){

				a[curx][cury]=a[x][y]+1;

				if(maks<a[curx][cury]) maks=a[curx][cury];

				q.push(make_pair(curx,cury));

			}

		}

	}

	cout<<maks<<endl;

}




// Faster C++ solution for the same problem (measured speedup: 1.1016681541650064x):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		scanf("%s",c[i]+1);

	queue<int>q;

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			if(c[i][j]=='#'){

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

		for(int i=0;i<4;i++){

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

	printf("%d\n",ans-1);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define fi first

#define se second

using namespace std;

int R, C;

char grid[1005][1005];

int dist[1005][1005];

queue<pair<int, int>> q;

int main() {

    ios_base::sync_with_stdio(0);

    cin.tie(0), cout.tie(0);

    cin >> R >> C;

    

    for (int i = 1; i <= R; i++) {

        for (int j = 1; j <= C; j++) {

            cin >> grid[i][j];

            if (grid[i][j] == '#') q.push({i, j});

            dist[i][j] = 1e9;

            // if (grid[i][j] == '#') dist[i][j] = 0;

        }

    }

    int step = 0, ans = 0;

    while (q.size()) {

        int sz = q.size();

        while (sz--) {

            pair<int, int> f = q.front(); q.pop();

            // cout << f.fi << " " << f.se << "\n";

            if (dist[f.fi][f.se] == 1e9) {

                if (dist[f.fi + 1][f.se] == 1e9 && f.fi < R) q.push({f.fi + 1, f.se});

                if (dist[f.fi - 1][f.se] == 1e9 && f.fi > 1) q.push({f.fi - 1, f.se});

                if (dist[f.fi][f.se + 1] == 1e9 && f.se < C) q.push({f.fi, f.se + 1});

                if (dist[f.fi][f.se - 1] == 1e9 && f.se > 1) q.push({f.fi, f.se - 1});

            }

            dist[f.fi][f.se] = min(dist[f.fi][f.se], step);

        }

        step++;

    }

    // cout << step << "\n";

    for (int i = 1; i <= R; i++) {

        for (int j = 1; j <= C; j++) {

            ans = max(ans, dist[i][j]);

        }

    }

    cout << ans << "\n";

}

// Faster C++ solution for the same problem (measured speedup: 1.1047735744707174x):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

	scanf("%d%d",&n,&m);

	for(int i=1;i<=n;i++)

		scanf("%s",c[i]+1);

	queue<int>q;

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			if(c[i][j]=='#'){

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

		for(int i=0;i<4;i++){

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

	printf("%d\n",ans-1);

}