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

using namespace std;

#define P pair<int,int>



int main() {

  int H, W;

  long long INF = 1000000000;

  char a[1005][1005];

  int S[1005][1005];

  queue<P>que;

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

  cin >> H >> W;

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

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

	  cin >> a[i][j];

      S[i][j] = INF;

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

	  	S[i][j] = 0;

		que.push({ i,j });

      }

	}

  }

  while (que.size()) {

	P p = que.front(); 

    que.pop();

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

		int nx = p.first + dx[i], ny = p.second + dy[i];

		if (nx < 0 || nx >= H || ny < 0 || ny >= W)continue;

		if (S[nx][ny] == INF) {

			S[nx][ny] = S[p.first][p.second] + 1;

			que.push({ nx,ny });

		  }

	  }

  }

  int ans = 0;

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

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

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

    }

  }

  cout << ans;

}


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

using namespace std;

#define P pair<int,int>



int main() {

  queue<P> que;

  int H, W;

  long long INF = 1000000000, x, y;

  cin >> H >> W;

  char a[H][W];

  int S[H][W], dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

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

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

      cin >> a[i][j];

      S[i][j] = INF;

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

        S[i][j] = 0;

        que.push({i, j});

      }

    }

  }

  while(!que.empty()) {

    P p = que.front();

    que.pop();

    x = p.first; y = p.second;

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

      int nx = x + dx[i], ny = y + dy[i];

      if (nx < 0 || nx >= H || ny < 0 || ny >= W)continue;

      if (S[nx][ny] == INF) {

		S[nx][ny] = S[x][y] + 1;

		que.push({ nx,ny });

      }

    }

  }

  int ans = 0;

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

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

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

    }

  }

  cout << ans;

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

#include<algorithm>

#include<vector>

#include<string>

#include<cmath>

#include<cstdio>

#include<queue>

#include<deque>

#include<map>

#include<stack>

#include<set>

#include<tuple>

using namespace std;

typedef pair<int,int> ii;

typedef long long ll;

typedef pair<ll,ll> pll;

typedef unsigned long long int ull;

const int INF=1e9;

const ll MOD=1e9+7;

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

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

const int MAXN=2e5;



int h,w;

vector<vector<char> > a(1000,vector<char>(1000));

vector<vector<int> > d(1000,vector<int>(1000,INF));



int main(){

    cin>>h>>w;

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

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

            cin>>a[i][j];

        }

    }

    int ans=0;

    queue<ii> que;

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

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

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

                d[i][j]=0;

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

            }

        }

    }

    while(!que.empty()){

        ii p=que.front();que.pop();

        int y=p.first,x=p.second;

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

            int ny=y+dy[i],nx=x+dx[i];

            if(ny>=0&&ny<h&&nx>=0&&nx<w&&d[ny][nx]>d[y][x]+1){

                d[ny][nx]=d[y][x]+1;

                que.push(make_pair(ny,nx));

                ans=max(ans,d[ny][nx]);

            }

        }

    }

    cout<<ans<<endl;

    return 0;

}

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

#include<algorithm>

#include<vector>

#include<string>

#include<cmath>

#include<cstdio>

#include<queue>

#include<deque>

#include<map>

#include<stack>

#include<set>

#include<utility>

using namespace std;

typedef pair<int,int> pii;

typedef pair<int,pii> p;

typedef long long ll;

typedef pair<ll,ll> pll;

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

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

const int MAXN=100000;

const int MAXE=100000;

const int MAXV=10000;

const ll INF=2e9;

const ll MOD=1e9+7;



int main(){

    int h,w;cin>>h>>w;

    vector<string> A(h);

    for(int i=0;i<h;++i) cin>>A[i];

    vector<vector<int> > d(h,vector<int>(w,INF));

    queue<pii> que;

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

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

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

                d[i][j]=0;

                que.push(pii(i,j));

            }

        }

    }

    int ans=0;

    while(!que.empty()){

        pii p=que.front();que.pop();

        int y=p.first,x=p.second;

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

            int ny=y+dy[i];

            int nx=x+dx[i];

            if(ny<0||ny>=h||nx<0||nx>=w) continue;

            if(d[ny][nx]!=INF) continue;

            d[ny][nx]=d[y][x]+1;

            que.push(pii(ny,nx));

            ans=max(ans,d[ny][nx]);

        }

    }

    cout<<ans<<endl;

    return 0;

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

using namespace std;

typedef long long ll;

typedef pair<int,int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

// #define debug(x) cout << #x << " : " << x;

const double PI = acos(-1);



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

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



char arr[1010][1010];

int cnt[1010][1010];

int h, w;



int main(){

	// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> h >> w;



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



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

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

			cin >> arr[i][j];



	queue<pair<ii, int > > q;



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

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

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

				q.push({{i, j}, 0});

			}

		}

	}



	int ans = 0;



	while (!q.empty()) {

		int u = q.front().first.first;

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

		int dist = q.front().second;

		q.pop();



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

			int tx = u + x[i];

			int ty = v + y[i];



			if (tx > 0 && tx <= h && ty > 0 && ty <= w) {

				if ((cnt[tx][ty] > dist + 1 || cnt[tx][ty] == -1) && arr[tx][ty] == '.') {

					cnt[tx][ty] = dist + 1;

					q.push({{tx, ty}, cnt[tx][ty]});

				}

			}

		}

	}



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

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

			// cout << cnt[i][j] << ' ' ;

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

		}

		// cout << endl;

	}



	cout << ans << endl;

}

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

using namespace std;



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

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



int main(){

	queue<pair<pair<int,int>,int> > bfs;

	int h,w;

	cin >> h >> w;

	char area[h+5][w+5];

	bool vis[h+5][w+5];

	memset(vis,0,sizeof vis);

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

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

			cin >> area[i][j];

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

				bfs.push({{i,j},0});

				vis[i][j]=1;

			}

		}

	}

	int ans=0;

	while(!bfs.empty()){

		int x=bfs.front().first.first;

		int y=bfs.front().first.second;

		int n=bfs.front().second;

		ans=max(ans,n);

		bfs.pop();

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

			if(x+mx[i]>=0 && y+my[i]>=0 && x+mx[i]<h && y+my[i]<w && !vis[x+mx[i]][y+my[i]]){

				bfs.push({{x+mx[i],y+my[i]},n+1});

				vis[x+mx[i]][y+my[i]]=1;

			}

		}

	}

	cout << ans << '\n';

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

using namespace std;

typedef long long ll;

typedef pair<int,int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

// #define debug(x) cout << #x << " : " << x;

const double PI = acos(-1);



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

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



char arr[1010][1010];

int cnt[1010][1010];

int h, w;



int main(){

	// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> h >> w;



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



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

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

			cin >> arr[i][j];



	queue<pair<ii, int > > q;



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

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

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

				q.push({{i, j}, 0});

			}

		}

	}



	int ans = 0;



	while (!q.empty()) {

		int u = q.front().first.first;

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

		int dist = q.front().second;

		q.pop();



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

			int tx = u + x[i];

			int ty = v + y[i];



			if (tx > 0 && tx <= h && ty > 0 && ty <= w) {

				if ((cnt[tx][ty] > dist + 1 || cnt[tx][ty] == -1) && arr[tx][ty] == '.') {

					cnt[tx][ty] = dist + 1;

					q.push({{tx, ty}, cnt[tx][ty]});

				}

			}

		}

	}



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

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

			// cout << cnt[i][j] << ' ' ;

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

		}

		// cout << endl;

	}



	cout << ans << endl;

}

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

#include <cmath>

#include <utility>

#include <queue>

#include <stack>

#include <algorithm>

#define fi first

#define se second

#define pb push_back

#define mp make_pair

#define vii vector < pair<int,int> >

#define pii pair<int,int>

const int INF=1e9+2;

const int eps=1e-9;

using namespace std;



int main(){

    int h, w, r, c, cnt, ans;

    char temp;

    bool grid[1002][1002];

    cin>>h>>w;

    queue<pair< pii, int> > bfs;

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

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

            cin>>temp;

            if (temp=='#'){

                grid[i][j]=1;

                bfs.push(pair<pii,int>(pii(i,j),0));

            }

            else grid[i][j]=0;

        }

    }

    while (!bfs.empty()){

        r=bfs.front().fi.fi;

        c=bfs.front().fi.se;

        cnt=bfs.front().se;

        bfs.pop();

        if (cnt!=0 && (r<1 || r>h || c<1 || c>w || grid[r][c])) continue;

        ans=cnt;

        //cout<<r<<c<<cnt<<endl;

        grid[r][c]=1;

        bfs.push(pair<pii,int>(pii(r+1,c),cnt+1));

        bfs.push(pair<pii,int>(pii(r,c+1),cnt+1));

        bfs.push(pair<pii,int>(pii(r,c-1),cnt+1));

        bfs.push(pair<pii,int>(pii(r-1,c),cnt+1));

    }

    cout<<ans;

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

using namespace std;

typedef long long ll;

typedef pair<int,int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

// #define debug(x) cout << #x << " : " << x;

const double PI = acos(-1);



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

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



char arr[1010][1010];

int cnt[1010][1010];

int h, w;



int main(){

	// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> h >> w;



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



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

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

			cin >> arr[i][j];



	queue<pair<ii, int > > q;



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

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

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

				q.push({{i, j}, 0});

			}

		}

	}



	int ans = 0;



	while (!q.empty()) {

		int u = q.front().first.first;

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

		int dist = q.front().second;

		q.pop();



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

			int tx = u + x[i];

			int ty = v + y[i];



			if (tx > 0 && tx <= h && ty > 0 && ty <= w) {

				if ((cnt[tx][ty] > dist + 1 || cnt[tx][ty] == -1) && arr[tx][ty] == '.') {

					cnt[tx][ty] = dist + 1;

					q.push({{tx, ty}, cnt[tx][ty]});

				}

			}

		}

	}



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

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

			// cout << cnt[i][j] << ' ' ;

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

		}

		// cout << endl;

	}



	cout << ans << endl;

}

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

using namespace std;



const int N = 1000 + 10;



queue< pair<int, int> > q;



int n, m;

int a[N][N];

char s[N][N];

int inq[N][N], d[N][N];



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

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



inline bool in (int x, int y) { return x > n || x < 1 || y > m || y < 1; }



int main (void) {

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

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

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

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

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

				q.push({ i, j });

				inq[i][j] = 1;

			}

		}

	}

	while (!q.empty()) {

		int i = q.front().first;

		int j = q.front().second;

		q.pop();

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

			if (inq[i + dx[k]][j + dy[k]]) continue;

			if (in(i + dx[k], j + dy[k])) continue;

			if (s[i + dx[k]][j + dy[k]] == '.') {

				inq[i + dx[k]][j + dy[k]] = 1;

				s[i + dx[k]][j + dy[k]] = '#';

				d[i + dx[k]][j + dy[k]] = d[i][j] + 1;

				q.push({ i + dx[k], j + dy[k] });

			}

		}

	}



	int mx = 0;

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

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

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

		}

	}

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

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

using namespace std;

typedef long long ll;

typedef pair<int,int> ii;

typedef vector<int> vi;

typedef vector<ii> vii;

// #define debug(x) cout << #x << " : " << x;

const double PI = acos(-1);



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

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



char arr[1010][1010];

int cnt[1010][1010];

int h, w;



int main(){

	// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

	cin >> h >> w;



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



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

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

			cin >> arr[i][j];



	queue<pair<ii, int > > q;



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

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

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

				q.push({{i, j}, 0});

			}

		}

	}



	int ans = 0;



	while (!q.empty()) {

		int u = q.front().first.first;

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

		int dist = q.front().second;

		q.pop();



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

			int tx = u + x[i];

			int ty = v + y[i];



			if (tx > 0 && tx <= h && ty > 0 && ty <= w) {

				if ((cnt[tx][ty] > dist + 1 || cnt[tx][ty] == -1) && arr[tx][ty] == '.') {

					cnt[tx][ty] = dist + 1;

					q.push({{tx, ty}, cnt[tx][ty]});

				}

			}

		}

	}



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

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

			// cout << cnt[i][j] << ' ' ;

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

		}

		// cout << endl;

	}



	cout << ans << endl;

}

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

#include <queue>



using namespace std;





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

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

int mat[1001][1001];

queue <pair<int,int>> v;

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

int n,m,maxi;



void lee(){

    int i,j,d,x,y;

    while(!q.empty()){

        i=q.front().first.first;

        j=q.front().first.second;

        d=q.front().second;

        q.pop();

        for(int _=0;_<=3;_++){

            x=i+dx[_];

            y=j+dy[_];

            if(1<=x && x<=n && 1<=y && y<=m){

                if(mat[x][y]>d){

                    mat[x][y]=d;

                    q.push({{x,y},d+1});

                }

            }

        }

    }

}

char c;

int i,j;



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>n>>m;

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

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

            cin>>c;

            if(c=='#'){

                v.push({i,j});

                mat[i][j]=-1;

            }

            else

                mat[i][j]=9999;

        }

    }

    while(!v.empty()){

        q.push({{v.front().first,v.front().second},1});

        v.pop();

    }

    ///

    lee();

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

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

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

    cout<<maxi<<'\n';

    return 0;

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

using namespace std;



const int MAXN = 1005;



int H, W, d[MAXN][MAXN];

char A[MAXN][MAXN];



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

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



int main(){	

	scanf("%d %d", &H, &W);

	

	memset(d, -1, sizeof d);

	

	queue < pair <int, int> > q;

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

		scanf("%s", A[i]);

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

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

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

				d[i][j] = 0;

			}

		}

	}

	

	int mx = 0;

	

	while(!q.empty()){

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

		q.pop();

		

		mx = max(mx, d[u.first][u.second]);

		

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

			int ui = u.first + di[dir], uj = u.second + dj[dir];

			

			if(ui < 0 || ui >= H || uj < 0 || uj >= W)continue;

			if(d[ui][uj] != -1)continue;

			

			q.push(make_pair(ui, uj));

			d[ui][uj] = d[u.first][u.second] + 1;

		}

	}

	

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

	

	return 0;

}


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

#include <queue>



using namespace std;





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

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

int mat[1001][1001];

queue <pair<int,int>> v;

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

int n,m,maxi;



void lee(){

    int i,j,d,x,y;

    while(!q.empty()){

        i=q.front().first.first;

        j=q.front().first.second;

        d=q.front().second;

        q.pop();

        for(int _=0;_<=3;_++){

            x=i+dx[_];

            y=j+dy[_];

            if(1<=x && x<=n && 1<=y && y<=m){

                if(mat[x][y]>d){

                    mat[x][y]=d;

                    q.push({{x,y},d+1});

                }

            }

        }

    }

}

char c;

int i,j;



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>n>>m;

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

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

            cin>>c;

            if(c=='#'){

                v.push({i,j});

                mat[i][j]=-1;

            }

            else

                mat[i][j]=9999;

        }

    }

    while(!v.empty()){

        q.push({{v.front().first,v.front().second},1});

        v.pop();

    }

    ///

    lee();

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

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

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

    cout<<maxi<<'\n';

    return 0;

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

using namespace std;

using uint = unsigned int;

using ll = long long;

using ld = long double;

using ull = unsigned long long;

using pii = pair<int, int>;

using pll = pair<ll, ll>;

#define dbg(x) cerr<<#x": "<<(x)<<endl

#define dbg_p(x) cerr<<#x": "<<(x).first<<' '<<(x).second<<endl

#define dbg_v(x, n) {cerr<<#x"[]: ";for(long long _=0;_<n;++_)cerr<<(x)[_]<<' ';cerr<<endl;}

#define all(v) v.begin(), v.end()

#define fi first

#define se second



template<typename T1, typename T2>

ostream& operator <<(ostream &out, const pair<T1, T2> &item) {

	out << '(' << item.first << ", " << item.second << ')';

	return out;

}



template <typename T>

ostream& operator <<(ostream &out, const vector<T>& v) {

	for(const auto &item : v) out << item << ' ';

	return out;

}



const int N = 1010;

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

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



char s[N];

int d[N][N];



int main()

{

	ios_base::sync_with_stdio(false);

	

	memset(d, -1, sizeof d);



	int m, n;

	queue<pii> q;



	cin >> m >> n;

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

		cin >> s;

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

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

	}



	int ans = 0;

	while(!q.empty()) {

		int x = q.front().fi;

		int y = q.front().se;

		q.pop();



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

			int a = x + dl[i];

			int b = y + dc[i];

			if(0 <= a && a < m && 0 <= b && b < n && d[a][b] == -1) {

				d[a][b] = 1 + d[x][y];

				q.push({a, b});

				ans = max(ans, d[a][b]);

			}

		}

	}



	cout << ans << '\n';

	return 0;

}


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

#include <queue>



using namespace std;





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

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

int mat[1001][1001];

queue <pair<int,int>> v;

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

int n,m,maxi;



void lee(){

    int i,j,d,x,y;

    while(!q.empty()){

        i=q.front().first.first;

        j=q.front().first.second;

        d=q.front().second;

        q.pop();

        for(int _=0;_<=3;_++){

            x=i+dx[_];

            y=j+dy[_];

            if(1<=x && x<=n && 1<=y && y<=m){

                if(mat[x][y]>d){

                    mat[x][y]=d;

                    q.push({{x,y},d+1});

                }

            }

        }

    }

}

char c;

int i,j;



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    cin>>n>>m;

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

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

            cin>>c;

            if(c=='#'){

                v.push({i,j});

                mat[i][j]=-1;

            }

            else

                mat[i][j]=9999;

        }

    }

    while(!v.empty()){

        q.push({{v.front().first,v.front().second},1});

        v.pop();

    }

    ///

    lee();

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

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

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

    cout<<maxi<<'\n';

    return 0;

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

using namespace std;



const int MAXN = 1005;



int H, W, d[MAXN][MAXN];

char A[MAXN][MAXN];



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

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



int main(){	

	scanf("%d %d", &H, &W);

	

	memset(d, -1, sizeof d);

	

	queue < pair <int, int> > q;

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

		scanf("%s", A[i]);

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

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

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

				d[i][j] = 0;

			}

		}

	}

	

	int mx = 0;

	

	while(!q.empty()){

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

		q.pop();

		

		mx = max(mx, d[u.first][u.second]);

		

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

			int ui = u.first + di[dir], uj = u.second + dj[dir];

			

			if(ui < 0 || ui >= H || uj < 0 || uj >= W)continue;

			if(d[ui][uj] != -1)continue;

			

			q.push(make_pair(ui, uj));

			d[ui][uj] = d[u.first][u.second] + 1;

		}

	}

	

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

	

	return 0;

}


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

# include <queue>

# include <climits>

using namespace std;

 

int H;

int W;

char A[1005][1005];

int counter=0;

int shortestpath[1005][1005];

bool visited[1005][1005];

int maximum=INT_MIN;

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

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

queue< pair<int, int> > starting;



bool inside (int row, int column)

{

    if (row<0 || row>H || column<0 || column>W)

    {

        return false;

    }



    return true;

}



void bfs (queue< pair<int, int> > starting)

{

    queue< pair<int, int> > q;



    while (!starting.empty())

    {

        q.push(make_pair(starting.front().first, starting.front().second));

        visited[starting.front().first][starting.front().second]=true;

        starting.pop();

    }



    while (!q.empty())

    {

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

        q.pop();



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

        {

            int nextrow=u.first+dr[i];

            int nextcolumn=u.second+dc[i];



            if (inside(nextrow, nextcolumn) && !visited[nextrow][nextcolumn])

            {

                shortestpath[nextrow][nextcolumn]=shortestpath[u.first][u.second]+1;

                q.push(make_pair(nextrow, nextcolumn));

                visited[nextrow][nextcolumn]=true;

            }

        }



    }

}



int main()

{

    cin>>H>>W;



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

    {

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

        {

            cin>>A[i][j];



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

            {

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

            }

        }

    }



    bfs(starting);



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

    {

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

        {

            if (shortestpath[i][j]>maximum)

            {

                maximum=shortestpath[i][j];

            }

        }

    }



    cout<<maximum<<endl;

}