stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int vis[1010][1010];

char ch[1010][1010];



int main() {

	memset(vis, -1, sizeof(vis));

	ios_base::sync_with_stdio(0);

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

	

	int x, y;

	cin >> x >> y;

	

	queue<pair<int, pair<int, int> > > q;

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

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

			cin >> ch[i][j];

			

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

				vis[i][j] = 0;

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

			}

		}

	}

	

	int ans = 0;

	while(!q.empty()) {

		int val = q.front().first;

		int tarX = q.front().second.first;

		int tarY = q.front().second.second;

		q.pop();

		

		ans = max(ans, val);

		if(tarX < x && vis[tarX + 1][tarY] == -1) {

			vis[tarX + 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX + 1, tarY)));

		}

		

		if(tarX > 1 && vis[tarX - 1][tarY] == -1) {

			vis[tarX - 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX - 1, tarY)));

		}

		

		if(tarY < y && vis[tarX][tarY + 1] == -1) {

			vis[tarX][tarY + 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY + 1)));

		}

		

		if(tarY > 1 && vis[tarX][tarY - 1] == -1) {

			vis[tarX][tarY - 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY - 1)));

		}

	}

	

	cout << ans << "\n";

}

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

using namespace std;

int n,m;

int a[1005][1005];

string s;

int main(){

	cin>>n>>m;

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

		cin>>s;

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

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

				a[i][j]=0;

			}

			else{

				a[i][j]=1000007;

			}

		}

	}

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

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

			if(i>0){

				a[i-1][j]=min(a[i][j]+1,a[i-1][j]);	

			}

			if(i<n-1){

				a[i+1][j]=min(a[i][j]+1,a[i+1][j]);

			}

			if(j>0){

				a[i][j-1]=min(a[i][j]+1,a[i][j-1]);

			}

			if(j<m-1){

				a[i][j+1]=min(a[i][j]+1,a[i][j+1]);	

			}

		}

	}

	for(int i=n-1;i>=0;i--){

		for(int j=m-1;j>=0;j--){

			if(i>0){

				a[i-1][j]=min(a[i][j]+1,a[i-1][j]);	

			}

			if(i<n-1){

				a[i+1][j]=min(a[i][j]+1,a[i+1][j]);

			}

			if(j>0){

				a[i][j-1]=min(a[i][j]+1,a[i][j-1]);

			}

			if(j<m-1){

				a[i][j+1]=min(a[i][j]+1,a[i][j+1]);	

			}

		}

	}

	int maks=0;

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

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

			maks=max(a[i][j],maks);

		}

	}

	cout<<maks<<endl;

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

#include <cstring>

#include <algorithm>

using namespace std;

#include <queue>

struct Node{

	int x,y;

	Node(int a,int b){

		x = a;

		y = b;

	}

	Node(){;}

};

 

queue<Node> q;

int N,M;

int xx[] = {0,0,0,1,-1};

int yy[] = {0,1,-1,0,0};

char str[1010][1010];

int F[1010][1010] = {0};

int main(){

	memset(F,0x3f,sizeof F);

	scanf("%d%d",&N,&M);

	for(int i = 1;i <= N;i++)scanf("%s",str[i]+1);

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

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

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

				F[i][j] = 0;

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

			}

		}

	}

	while(!q.empty()){

		Node D= q.front();

		q.pop();

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

			int x = D.x + xx[i],y = D.y + yy[i];

			if(x < 1 || y < 1 || x > N || y > M)continue;

			if(F[x][y] > F[D.x][D.y] + 1){

				F[x][y] = F[D.x][D.y] + 1;

				q.push(Node(x,y));

			}

		}

	}

	int ans = 0;

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

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

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

		}

	}

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

	return 0;

}

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

#include <queue>

using namespace std;

int n,m,i,j,a[1005][1005];

int maxi;

char x;

struct now

{

    int x,y,len;

    now(int x, int y, int len)

    {

        this->x=x;

        this->y=y;

        this->len=len;

    }

};

queue <now> q;

int main()

{

    cin>>n>>m;

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

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

    {

        cin>>x;

        if(x=='#')

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

    }

    while(!q.empty())

    {

        auto poz = q.front();

        if(poz.x<1 || poz.x>n || poz.y<1 || poz.y>m || a[poz.x][poz.y]==1)

        {

            q.pop();

            continue;

        }

        a[poz.x][poz.y]=1;

        maxi=poz.len;

        q.push(now(poz.x+1, poz.y, poz.len+1));

        q.push(now(poz.x-1, poz.y, poz.len+1));

        q.push(now(poz.x, poz.y+1, poz.len+1));

        q.push(now(poz.x, poz.y-1, poz.len+1));

        q.pop();

    }

    cout<<maxi;

    return 0;

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

using namespace std;



int vis[1010][1010];

char ch[1010][1010];



int main() {

	memset(vis, -1, sizeof(vis));

	ios_base::sync_with_stdio(0);

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

	

	int x, y;

	cin >> x >> y;

	

	queue<pair<int, pair<int, int> > > q;

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

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

			cin >> ch[i][j];

			

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

				vis[i][j] = 0;

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

			}

		}

	}

	

	int ans = 0;

	while(!q.empty()) {

		int val = q.front().first;

		int tarX = q.front().second.first;

		int tarY = q.front().second.second;

		q.pop();

		

		ans = max(ans, val);

		if(tarX < x && vis[tarX + 1][tarY] == -1) {

			vis[tarX + 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX + 1, tarY)));

		}

		

		if(tarX > 1 && vis[tarX - 1][tarY] == -1) {

			vis[tarX - 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX - 1, tarY)));

		}

		

		if(tarY < y && vis[tarX][tarY + 1] == -1) {

			vis[tarX][tarY + 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY + 1)));

		}

		

		if(tarY > 1 && vis[tarX][tarY - 1] == -1) {

			vis[tarX][tarY - 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY - 1)));

		}

	}

	

	cout << ans << "\n";

}

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

#include <queue>

using namespace std;

int n,m,i,j,a[1005][1005];

int maxi;

char x;

struct now

{

    int x,y,len;

    now(int x, int y, int len)

    {

        this->x=x;

        this->y=y;

        this->len=len;

    }

};

queue <now> q;

int main()

{

    cin>>n>>m;

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

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

    {

        cin>>x;

        if(x=='#')

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

    }

    while(!q.empty())

    {

        auto poz = q.front();

        if(poz.x<1 || poz.x>n || poz.y<1 || poz.y>m || a[poz.x][poz.y]==1)

        {

            q.pop();

            continue;

        }

        a[poz.x][poz.y]=1;

        maxi=poz.len;

        q.push(now(poz.x+1, poz.y, poz.len+1));

        q.push(now(poz.x-1, poz.y, poz.len+1));

        q.push(now(poz.x, poz.y+1, poz.len+1));

        q.push(now(poz.x, poz.y-1, poz.len+1));

        q.pop();

    }

    cout<<maxi;

    return 0;

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

#include <cstring>

#include <algorithm>

using namespace std;

#include <queue>

struct Node{

	int x,y;

	Node(int a,int b){

		x = a;

		y = b;

	}

	Node(){;}

};

 

queue<Node> q;

int N,M;

int xx[] = {0,0,0,1,-1};

int yy[] = {0,1,-1,0,0};

char str[1010][1010];

int F[1010][1010] = {0};

int main(){

	memset(F,0x3f,sizeof F);

	scanf("%d%d",&N,&M);

	for(int i = 1;i <= N;i++)scanf("%s",str[i]+1);

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

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

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

				F[i][j] = 0;

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

			}

		}

	}

	while(!q.empty()){

		Node D= q.front();

		q.pop();

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

			int x = D.x + xx[i],y = D.y + yy[i];

			if(x < 1 || y < 1 || x > N || y > M)continue;

			if(F[x][y] > F[D.x][D.y] + 1){

				F[x][y] = F[D.x][D.y] + 1;

				q.push(Node(x,y));

			}

		}

	}

	int ans = 0;

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

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

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

		}

	}

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

	return 0;

}

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

using namespace std;



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    int m, n;

    cin >> m >> n;



    queue<pair<int, int>> q;

    vector<vector<int>> dp(m + 2, vector<int>(n + 2));

    vector<vector<char>> mat(m + 2, vector<char>(n + 2));

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

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

            cin >> mat[i][j];

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

                q.emplace(i, j);

                dp[i][j] = 1;

            }

        }



    int addLin[] = {-1, 0, 1, 0};

    int addCol[] = {0, 1, 0, -1};



    while (!q.empty()) {

        auto top = q.front(); q.pop();

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

            int x = top.first + addLin[i];

            int y = top.second + addCol[i];

            if (mat[x][y] == '.' && !dp[x][y]) {

                dp[x][y] = dp[top.first][top.second] + 1;

                q.emplace(x, y);

            }

        }

    }



    int mx = 0;

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

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

            mx = max(mx, dp[i][j]);



    cout << mx - 1 << '\n';

    return 0;

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

using namespace std;



int vis[1010][1010];

char ch[1010][1010];



int main() {

	memset(vis, -1, sizeof(vis));

	ios_base::sync_with_stdio(0);

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

	

	int x, y;

	cin >> x >> y;

	

	queue<pair<int, pair<int, int> > > q;

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

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

			cin >> ch[i][j];

			

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

				vis[i][j] = 0;

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

			}

		}

	}

	

	int ans = 0;

	while(!q.empty()) {

		int val = q.front().first;

		int tarX = q.front().second.first;

		int tarY = q.front().second.second;

		q.pop();

		

		ans = max(ans, val);

		if(tarX < x && vis[tarX + 1][tarY] == -1) {

			vis[tarX + 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX + 1, tarY)));

		}

		

		if(tarX > 1 && vis[tarX - 1][tarY] == -1) {

			vis[tarX - 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX - 1, tarY)));

		}

		

		if(tarY < y && vis[tarX][tarY + 1] == -1) {

			vis[tarX][tarY + 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY + 1)));

		}

		

		if(tarY > 1 && vis[tarX][tarY - 1] == -1) {

			vis[tarX][tarY - 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY - 1)));

		}

	}

	

	cout << ans << "\n";

}

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

using namespace std;



int main() {

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);



    int m, n;

    cin >> m >> n;



    queue<pair<int, int>> q;

    vector<vector<int>> dp(m + 2, vector<int>(n + 2));

    vector<vector<char>> mat(m + 2, vector<char>(n + 2));

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

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

            cin >> mat[i][j];

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

                q.emplace(i, j);

                dp[i][j] = 1;

            }

        }



    int addLin[] = {-1, 0, 1, 0};

    int addCol[] = {0, 1, 0, -1};



    while (!q.empty()) {

        auto top = q.front(); q.pop();

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

            int x = top.first + addLin[i];

            int y = top.second + addCol[i];

            if (mat[x][y] == '.' && !dp[x][y]) {

                dp[x][y] = dp[top.first][top.second] + 1;

                q.emplace(x, y);

            }

        }

    }



    int mx = 0;

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

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

            mx = max(mx, dp[i][j]);



    cout << mx - 1 << '\n';

    return 0;

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

#include <cstring>

#include <algorithm>

using namespace std;

#include <queue>

struct Node{

	int x,y;

	Node(int a,int b){

		x = a;

		y = b;

	}

	Node(){;}

};

 

queue<Node> q;

int N,M;

int xx[] = {0,0,0,1,-1};

int yy[] = {0,1,-1,0,0};

char str[1010][1010];

int F[1010][1010] = {0};

int main(){

	memset(F,0x3f,sizeof F);

	scanf("%d%d",&N,&M);

	for(int i = 1;i <= N;i++)scanf("%s",str[i]+1);

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

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

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

				F[i][j] = 0;

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

			}

		}

	}

	while(!q.empty()){

		Node D= q.front();

		q.pop();

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

			int x = D.x + xx[i],y = D.y + yy[i];

			if(x < 1 || y < 1 || x > N || y > M)continue;

			if(F[x][y] > F[D.x][D.y] + 1){

				F[x][y] = F[D.x][D.y] + 1;

				q.push(Node(x,y));

			}

		}

	}

	int ans = 0;

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

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

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

		}

	}

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

	return 0;

}

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

#define pb push_back



using namespace std;



void debug_out() { cerr << '\n'; }

template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...);}

#define dbg(...) cerr << #__VA_ARGS__ << " ->", debug_out(__VA_ARGS__)

#define dbg_v(x, n) do{cerr<<#x"[]: ";for(int _=0;_<n;++_)cerr<<x[_]<<" ";cerr<<'\n';}while(0)

#define dbg_ok cerr<<"OK!\n"



typedef pair<int,int> pii;

typedef long long int ll;

typedef long double ld;



inline int two(int n) { return 1 << n; }

inline int test(int n, int pos) { return (bool) (n & two(pos)); }

inline void set_bit(int &n, int b) { n |= two(b); }

inline void unset_bit(int &n, int b) { n &= ~two(b); }

inline int last_bit(int n) { return n & (-n); }



const int DMAX = 1e3+10;



struct nume{

    int node,val;

};



vector <int> arb[DMAX];



queue <pii> Coada;



bool uz[DMAX][DMAX];

int M[DMAX][DMAX];



int dl[]={1,-1,0,0};

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



int n,ans,m;



void lee();



int main(){

    ios_base::sync_with_stdio(false);

    cin.tie(0);



    int t,i,j,val;

    char c;



    cin>>n>>m;

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

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

            cin>>c;

            if(c == '#'){

                Coada.push({i,j});

                uz[i][j]=true;

            }

        }

    lee();

    cout<<ans<<'\n';

    return 0;

}

void lee(){

    int i,k,x,y;

    pii node;

    while(!Coada.empty()){

            node=Coada.front();

            Coada.pop();

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

                x=node.first+dl[k];

                y=node.second+dc[k];

                if(x>=1 && x<=n && y>=1 && y<=m && !uz[x][y]){

                   Coada.push({x,y});

                   M[x][y]=M[node.first][node.second]+1;

                   uz[x][y]=true;

                   ans=max(ans,M[x][y]);

                }

            }

      }

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

#include <cstring>

#include <algorithm>

using namespace std;

#include <queue>

struct Node{

	int x,y;

	Node(int a,int b){

		x = a;

		y = b;

	}

	Node(){;}

};

 

queue<Node> q;

int N,M;

int xx[] = {0,0,0,1,-1};

int yy[] = {0,1,-1,0,0};

char str[1010][1010];

int F[1010][1010] = {0};

int main(){

	memset(F,0x3f,sizeof F);

	scanf("%d%d",&N,&M);

	for(int i = 1;i <= N;i++)scanf("%s",str[i]+1);

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

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

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

				F[i][j] = 0;

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

			}

		}

	}

	while(!q.empty()){

		Node D= q.front();

		q.pop();

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

			int x = D.x + xx[i],y = D.y + yy[i];

			if(x < 1 || y < 1 || x > N || y > M)continue;

			if(F[x][y] > F[D.x][D.y] + 1){

				F[x][y] = F[D.x][D.y] + 1;

				q.push(Node(x,y));

			}

		}

	}

	int ans = 0;

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

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

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

		}

	}

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

	return 0;

}

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

#include <cstring>

#include <algorithm>

using namespace std;



string str;

int i, ok1;

int k, t, j, lg, n, m, rez;

char a[1001][1001];

int mn=9999999;

int mat[1001][1001];



struct coada

{

    int x, y, dist;

}C[1000001], aux;



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

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

int inc, fin, x, y;

int main()

{

    cin>>n>>m;

    cin.get();

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

        cin.getline(a[i], 1001);



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

        mat[i][0]=mat[i][m+1]=-1;

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

        mat[0][j]=mat[n+1][j]=-1;

    inc=-1;

    fin=0;

    int nr=0;

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

        for (j=0;j<=m;j++)

        {

            if (a[i][j]=='#')

            {

                C[nr].x=i;

                C[nr].y=j+1;

                C[nr].dist=1;

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

                nr++;

            }

        }

    fin=nr-1;

    while (fin>inc)

    {

        inc++;

        aux=C[inc];

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

        {

            x=aux.x+dx[k];

            y=aux.y+dy[k];

            if (mat[x][y]==0)

            {

                mat[x][y]=mat[aux.x][aux.y]+1;

                fin++;

                C[fin].x=x;

                C[fin].y=y;

                C[fin].dist=mat[x][y];

            }



        }

    }

    int mx=0;

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

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

        if (mx<mat[i][j]) mx=mat[i][j];

    cout<<mx-1<<'\n';

    return 0;

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

using namespace std;



int vis[1010][1010];

char ch[1010][1010];



int main() {

	memset(vis, -1, sizeof(vis));

	ios_base::sync_with_stdio(0);

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

	

	int x, y;

	cin >> x >> y;

	

	queue<pair<int, pair<int, int> > > q;

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

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

			cin >> ch[i][j];

			

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

				vis[i][j] = 0;

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

			}

		}

	}

	

	int ans = 0;

	while(!q.empty()) {

		int val = q.front().first;

		int tarX = q.front().second.first;

		int tarY = q.front().second.second;

		q.pop();

		

		ans = max(ans, val);

		if(tarX < x && vis[tarX + 1][tarY] == -1) {

			vis[tarX + 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX + 1, tarY)));

		}

		

		if(tarX > 1 && vis[tarX - 1][tarY] == -1) {

			vis[tarX - 1][tarY] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX - 1, tarY)));

		}

		

		if(tarY < y && vis[tarX][tarY + 1] == -1) {

			vis[tarX][tarY + 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY + 1)));

		}

		

		if(tarY > 1 && vis[tarX][tarY - 1] == -1) {

			vis[tarX][tarY - 1] = val + 1;

			q.push(make_pair(val + 1, make_pair(tarX, tarY - 1)));

		}

	}

	

	cout << ans << "\n";

}

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

#include <cstring>

#include <algorithm>

using namespace std;



string str;

int i, ok1;

int k, t, j, lg, n, m, rez;

char a[1001][1001];

int mn=9999999;

int mat[1001][1001];



struct coada

{

    int x, y, dist;

}C[1000001], aux;



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

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

int inc, fin, x, y;

int main()

{

    cin>>n>>m;

    cin.get();

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

        cin.getline(a[i], 1001);



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

        mat[i][0]=mat[i][m+1]=-1;

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

        mat[0][j]=mat[n+1][j]=-1;

    inc=-1;

    fin=0;

    int nr=0;

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

        for (j=0;j<=m;j++)

        {

            if (a[i][j]=='#')

            {

                C[nr].x=i;

                C[nr].y=j+1;

                C[nr].dist=1;

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

                nr++;

            }

        }

    fin=nr-1;

    while (fin>inc)

    {

        inc++;

        aux=C[inc];

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

        {

            x=aux.x+dx[k];

            y=aux.y+dy[k];

            if (mat[x][y]==0)

            {

                mat[x][y]=mat[aux.x][aux.y]+1;

                fin++;

                C[fin].x=x;

                C[fin].y=y;

                C[fin].dist=mat[x][y];

            }



        }

    }

    int mx=0;

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

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

        if (mx<mat[i][j]) mx=mat[i][j];

    cout<<mx-1<<'\n';

    return 0;

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

#include <cstring>

#include <algorithm>

using namespace std;

#include <queue>

struct Node{

	int x,y;

	Node(int a,int b){

		x = a;

		y = b;

	}

	Node(){;}

};

 

queue<Node> q;

int N,M;

int xx[] = {0,0,0,1,-1};

int yy[] = {0,1,-1,0,0};

char str[1010][1010];

int F[1010][1010] = {0};

int main(){

	memset(F,0x3f,sizeof F);

	scanf("%d%d",&N,&M);

	for(int i = 1;i <= N;i++)scanf("%s",str[i]+1);

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

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

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

				F[i][j] = 0;

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

			}

		}

	}

	while(!q.empty()){

		Node D= q.front();

		q.pop();

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

			int x = D.x + xx[i],y = D.y + yy[i];

			if(x < 1 || y < 1 || x > N || y > M)continue;

			if(F[x][y] > F[D.x][D.y] + 1){

				F[x][y] = F[D.x][D.y] + 1;

				q.push(Node(x,y));

			}

		}

	}

	int ans = 0;

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

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

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

		}

	}

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

	return 0;

}

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



using namespace std;



int dist[2000][2000];

bool vis[2000][2000];

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);



    int H, W;

    cin >> H >> W;

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

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

            dist[i][j] = 10000, vis[i][j] = false;

    queue<pair<int,int> > Q;

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

    {

        string s;

        cin >> s;

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

        {

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

            {

                dist[i][j] = 0;

                vis[i][j] = true;

                Q.push({i, j});

            }

        }

    }

    auto inside = [&](int i, int j){

        if (i < 0 || i >= H || j < 0 || j >= W)

            return 0;

        return 1;

    };

    while(!Q.empty())

    {

        int i = Q.front().first;

        int j = Q.front().second;

        Q.pop();

        vector<pair<int,int> > ngbhs = {{i + 1, j}, {i - 1, j}, {i, j + 1}, {i, j - 1}};

        for(auto f : ngbhs)

        {

            if (inside(f.first, f.second))



                if(!vis[f.first][f.second])

                {

                    vis[f.first][f.second] = true;

                    dist[f.first][f.second] = min(dist[i][j] + 1, dist[f.first][f.second]);

                    Q.push(f);

                }

        }

    }

    int ans = 0;

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

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

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

    cout << ans << '\n';

    return 0;

}