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.079181336172244x):
#include<iostream>

#include<math.h>

#include<iomanip>

#include <string>

#include <cstdio>

#include<stdio.h>

#include <cstring>

#include <algorithm>

#include <queue>

#define ll long long

using namespace std;int stop;

int n,m;

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

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

int black;

int map[1002][1002];

bool isfull[1002][1002];

struct Node

{

    int x,y;

    int step;

    Node(int a,int b,int c){

        x=a,y=b,step = c;

    }

};



int check(int x,int y,int k){

    int sum = 0;

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

        if(map[x+dx[i]][y+dy[i]] == 999999){

            map[x+dx[i]][y+dy[i]] = k;

            sum++;

        }

    }

    return sum;

}



int main(){

    int i,j,k,x;

    char ch;

    queue<Node>q;

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

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

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

            cin>>ch;

            if(ch == '#'){

                map[i][j] = 0;

                black++;

                q.push(Node(i,j,0));

            }else{

                map[i][j] = 999999;

            }

        }

    }

    if(black == n*m){

        printf("0\n");

        return 0;

    }

    

    int ans = 0;

    while (!q.empty())

    {

        Node u = q.front();q.pop();

        for(k=0;k<4;k++){

            if(map[u.x + dx[k]][u.y+dy[k]] == 999999){

                map[u.x + dx[k]][u.y+dy[k]] = 0;

                q.push(Node(u.x + dx[k],u.y+dy[k],u.step+1));

                ans = max(ans,u.step+1);

            }

        }

    }

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

    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: 9.520797671493606x):
#include<iostream>

#include<math.h>

#include<iomanip>

#include <string>

#include <cstdio>

#include<stdio.h>

#include <cstring>

#include <algorithm>

#include <queue>

#define ll long long

using namespace std;int stop;

int n,m;

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

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

int black;

int map[1002][1002];

bool isfull[1002][1002];

struct Node

{

    int x,y;

    int step;

    Node(int a,int b,int c){

        x=a,y=b,step = c;

    }

};



int check(int x,int y,int k){

    int sum = 0;

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

        if(map[x+dx[i]][y+dy[i]] == 999999){

            map[x+dx[i]][y+dy[i]] = k;

            sum++;

        }

    }

    return sum;

}



int main(){

    int i,j,k,x;

    char ch;

    queue<Node>q;

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

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

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

            cin>>ch;

            if(ch == '#'){

                map[i][j] = 0;

                black++;

                q.push(Node(i,j,0));

            }else{

                map[i][j] = 999999;

            }

        }

    }

    if(black == n*m){

        printf("0\n");

        return 0;

    }

    

    int ans = 0;

    while (!q.empty())

    {

        Node u = q.front();q.pop();

        for(k=0;k<4;k++){

            if(map[u.x + dx[k]][u.y+dy[k]] == 999999){

                map[u.x + dx[k]][u.y+dy[k]] = 0;

                q.push(Node(u.x + dx[k],u.y+dy[k],u.step+1));

                ans = max(ans,u.step+1);

            }

        }

    }

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

    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.121924405141199x):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;



int h, w, cnt = 0, color = 1;



int a[1010][1010];

struct node {

    int i, j;

} res[1000010];



void change(int i, int j) {

    if (i - 1 >= 1 && !a[i - 1][j]) {

        a[i - 1][j] = color;

        res[cnt].i = i - 1;

        res[cnt++].j = j;

    }

    if (i + 1 <= h && !a[i + 1][j]) {

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

        res[cnt].i = i + 1;

        res[cnt++].j = j;

    }

    if (j - 1 >= 1 && !a[i][j - 1]) {

        a[i][j - 1] = color;

        res[cnt].i = i;

        res[cnt++].j = j - 1;

    }

    if (j + 1 <= w && !a[i][j + 1]) {

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

        res[cnt].i = i;

        res[cnt++].j = j + 1;

    }

}



int main() {

    int pos = 0;

    char temp;

    cin >> h >> w;

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

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

            cin >> temp;

            if (temp == '.')

                a[i][j] = 0;

            else {

                a[i][j] = -1;

                res[cnt].i = i;

                res[cnt++].j = j;

            }

        }

    }

    while (cnt < h * w) {

        int num = cnt;

        for (; pos < num; pos++) {

            change(res[pos].i, res[pos].j);

        }

        color++;

    }



    cout << color - 1 << 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: 9.600918479272993x):
#include <bits/stdc++.h>



using namespace std;

typedef long long ll;



int h, w, cnt = 0, color = 1;



int a[1010][1010];

struct node {

    int i, j;

} res[1000010];



void change(int i, int j) {

    if (i - 1 >= 1 && !a[i - 1][j]) {

        a[i - 1][j] = color;

        res[cnt].i = i - 1;

        res[cnt++].j = j;

    }

    if (i + 1 <= h && !a[i + 1][j]) {

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

        res[cnt].i = i + 1;

        res[cnt++].j = j;

    }

    if (j - 1 >= 1 && !a[i][j - 1]) {

        a[i][j - 1] = color;

        res[cnt].i = i;

        res[cnt++].j = j - 1;

    }

    if (j + 1 <= w && !a[i][j + 1]) {

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

        res[cnt].i = i;

        res[cnt++].j = j + 1;

    }

}



int main() {

    int pos = 0;

    char temp;

    cin >> h >> w;

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

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

            cin >> temp;

            if (temp == '.')

                a[i][j] = 0;

            else {

                a[i][j] = -1;

                res[cnt].i = i;

                res[cnt++].j = j;

            }

        }

    }

    while (cnt < h * w) {

        int num = cnt;

        for (; pos < num; pos++) {

            change(res[pos].i, res[pos].j);

        }

        color++;

    }



    cout << color - 1 << 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.013573585008911x):
#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;

}
// 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.39781759199549x):
#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;

}
// 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: 4.999480875357956x):
#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";

}
// 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.371401162191283x):
#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";

}