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

using namespace std;

using ll = long long;

using pii = pair<int,int>;

#define rep(i,n) for(int i=0; i<n; i++)

#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end()) //sortしてから使う

#define INF 2147483647

#define LLINF 9223372036854775807LL



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



int main(){

  int h,w;

  cin>>h>>w;

  char C[1010][1010];

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

    for (int j=1; j<=w; j++) cin >> C[i][j];

  }

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



  queue<pii> Q;

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

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

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

        V[i][j] = 1;

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

      }

    }

  }



  while(!Q.empty()){

    int tmp_h = Q.front().first;

    int tmp_w = Q.front().second;

    Q.pop();

    rep(i,4){

      if (V[tmp_h+d[i]][tmp_w+d[i+1]] == 0 && tmp_h+d[i] >= 1 && tmp_h+d[i] && tmp_h+d[i] <= h

        && tmp_w+d[i+1] >= 1 && tmp_w+d[i+1] <= w){

        V[tmp_h+d[i]][tmp_w+d[i+1]] = V[tmp_h][tmp_w] + 1;

        Q.push(make_pair(tmp_h+d[i], tmp_w+d[i+1]));

      }

    }

  }

  int ans = -1;

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

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

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

    }

  }

  cout << ans - 1 << endl;

}


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

#define rep(i,n) for(int i=0;i<n;i++)

using namespace std;

using ll = long long;

using p = pair<int,int>;

using Graph = vector<vector<int>>;

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

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

int main(int argc, const char * argv[]) {



    cin.tie(0);

    ios::sync_with_stdio(false);



    int h,w;

    cin>>h>>w;

    char maze[h][w];

    vector<vector<int>>black(h,vector<int>(w,-1));

    queue<p>que;

    rep(i,h){

        rep(j,w){

            cin>>maze[i][j];

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

                que.emplace(i,j);

                black[i][j]=0;

            }

        }

    }

    int ans=0;

    while(!que.empty()){

        int x,y;

        tie(y,x)=que.front();

        que.pop();

        ans=black[y][x];

        rep(i,4){

            int a=x+dx[i];

            int b=y+dy[i];

            if(a<0||a>=w||b<0||b>=h)continue;

            if(black[b][a]!=-1)continue;

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

            que.emplace(b,a);

        }

    }

    cout<<ans<<endl;





    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: 5.100937765690626x):
#include <stdio.h>

#include <string.h>

#include <iostream>

#include <queue>

#include <ctype.h>

using namespace std;

int n,m,a,b,c;



struct Node

{

    int x,y,z;

    Node(){}

    Node(int a,int b,int c): x(a), y(b),z(c) {}

};



bool vis[1005][1005];

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

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



char ma[1005][1005];





int bfs()

{

    queue<Node>Q;

    int ans=0;

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

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

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

    {

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

    	ans++;

    	vis[i][j]=1;

    }

    

    if(ans==n*m)

    return 0;

    

    while(!Q.empty())

    {

        Node node=Q.front(); 

		Q.pop();

        int x=node.x,y=node.y,z=node.z; 

        for(int i=0;i<4;i++)//四个方向 

        {

            int nx=x+dirx[i],ny=y+diry[i],nz=z+1;

            

            if(nx<1||nx>n||ny<1||ny>m) continue;

            if(vis[nx][ny]) continue;

//            printf("%d %d",ans,nz);

            vis[nx][ny]=1;

            ans++;

            if(ans==n*m)

            return nz;

            Q.push(Node(nx,ny,nz));

        }

    }

}





int main()

{

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

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

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

    

    int t = bfs();

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

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

typedef long long ll;

typedef unsigned long long ull;

typedef double db;

const int MAXN = 1e3+5,MAXM = 2e5+5,MOD = 20130427,INF = 0x3f3f3f3f,N=100050;

const ll INFL = 0x3f3f3f3f3f3f3f3f;

const db eps = 1e-9;

#define lson o<<1,l,m

#define rson o<<1|1,m+1,r

#define mid l + ((r-l)>>1)

#define rep(i,a,b) for(register int i=(a);i<=(b);i++)

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

#define vi vector<int>

using namespace std;

struct node{

    int x,y,step;

}cur,nxt;

int n,m;

char mp[MAXN][MAXN];

bool vis[MAXN][MAXN];

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

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

inline bool bound(int x,int y){

    return x>=1&&x<=n&&y>=1&&y<=m;

}

int main(){

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

    //freopen("../A.in","r",stdin);

    //freopen("../A.out","w",stdout);

    cin>>n>>m;

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

        cin>>(mp[i]+1);

    }

    int ans=0;

    queue<node> q;

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

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

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

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

                vis[i][j]=1;

            }

        }

    }

    while(q.size()){

        cur =q.front();q.pop();

        ans=max(ans,cur.step);

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

            nxt.x=cur.x+dx[i];

            nxt.y=cur.y+dy[i];

            nxt.step=cur.step+1;

            if(bound(nxt.x,nxt.y)&&!vis[nxt.x][nxt.y]&&mp[nxt.x][nxt.y]=='.'){

                vis[nxt.x][nxt.y]=1;

                q.push(nxt);

            }

        }

    }

    cout<<ans<<'\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.976308843246323x):
#include <bits/stdc++.h>

#define MP make_first

#define fi first

#define se second

using namespace std;

typedef pair<int, int> pii;

typedef long long ll;

const int N = 1005;



int n, m;

char mp[N][N];



struct node{

    int x, y, now;

};



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



bool ok(int x, int y) {

    return x >= 1 && x <= n && y >= 1 && y <= m && mp[x][y] == '.';

}



int main() {

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

    cin >> n >> m;

    queue <node> q;

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

        cin >> mp[i] + 1;

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

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

        }

    }

    int ans = 0;

    while(!q.empty()) {

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

        int x = cur.x, y = cur.y;

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

            int curx = x + dx[i], cury = y + dy[i];

            int now = cur.now;

            ans = now;

            if(ok(curx, cury)) {

                q.push(node{curx, cury, now + 1});

                mp[curx][cury] = '#';

            }

        }

    }

    cout << ans;

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

#include <queue>

#define pii pair<int,int>

#define ppi pair<pii,int>

#define fst first

#define snd second

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

using namespace std;

queue<ppi> q;int n,m,mx=0;string mp[1001]={};bool bt[1001][1001]={};

bool isv(pii dt) {return dt.fst>=0&&dt.fst<n&&dt.snd>=0&&dt.snd<m;}

int main()

{

	cin>>n>>m;

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

	{

		cin>>mp[i];

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

		{

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

		}

	}

	while (!q.empty())

	{

		ppi dt = q.front();q.pop();

		if (!bt[dt.fst.fst][dt.fst.snd])

		{

			bt[dt.fst.fst][dt.fst.snd] = 1;

			mx = dt.snd;

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

			{

				if (isv({dt.fst.fst+dr[i][0],dt.fst.snd+dr[i][1]})) {q.push({{dt.fst.fst+dr[i][0],dt.fst.snd+dr[i][1]},dt.snd+1});}

			}

		}

	}

	cout<<mx<<"\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: 5.045052724368825x):
#include <bits/stdc++.h>



using namespace std;



#define mp make_pair

#define fr first

#define sc second



long long n,m,ky[4]={-1,0,1,0},kx[4]={0,1,0,-1};

bitset<1069> a[1069];

queue<pair<pair<long long,long long>,long long>> q;



int main()

{

	long long i,j,it,y,x,w,yy,xx;

	string s;

	

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

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

	{

		cin>>s;

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

		{

			if(s[j-1]=='.')

			{

				a[i][j]=0;

			}

			else

			{

				a[i][j]=1;

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

			}

		}

	}

	for(;!q.empty();)

	{

		y=q.front().fr.fr;

		x=q.front().fr.sc;

		w=q.front().sc;

		q.pop();

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

		{

			yy=y+ky[it];

			xx=x+kx[it];

			if(yy>0&&xx>0&&yy<=n&&xx<=m&&!a[yy][xx])

			{

				q.push(mp(mp(yy,xx),w+1));

				a[yy][xx]=1;

			}

		}

	}

	printf("%lld\n",w);

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



using namespace std;



#define pb push_back

#define MEM(a, x) memset(a, x, sizeof a)



typedef pair<int, int> ii;



int main() {

	int h, w;

	char buf;

	bool grid[1005][1005];

	vector<ii> st;



	cin >> h >> w;

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

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

			cin >> buf;

			grid[i][j] = (buf == '#') ? true : false;

			if(grid[i][j])

				st.pb({i,j});

		}

	}



	queue<ii> q;

	int u, d, ans;



	for(auto& it : st) {

		int idx = it.first * w + it.second;

		q.push({idx, 0});

	}



	while(!q.empty()) {

		u = q.front().first; d = q.front().second;

		q.pop();



		int i = u / w, j = u % w;



		if(!grid[i][j] || d == 0) {

			grid[i][j] = true;

			ans = d;

			//cout << i << ' ' << j << ' ' << d << endl;



			if(i < h - 1 && !grid[i+1][j]) q.push({(i+1) * w + j, d + 1});

			if(i > 0 && !grid[i-1][j]) q.push({(i-1) * w + j, d + 1});

			if(j < w - 1 && !grid[i][j+1]) q.push({i * w + j + 1, d + 1});

			if(j > 0 && !grid[i][j-1]) q.push({i * w + j - 1, d + 1});

		}

	}



	cout << ans << endl;



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



using namespace std;



bool visited[1010][1010];

int angka[1010][1010];

char matrix[1010][1010];

long long maks,b,k,i,j;

pair<pair<int,int>,int> temp,sekarang;

pair<int,int> posisi;

queue <pair<pair<int,int>,int> > antri;

int main()

{

	cin >> b >> k;

	for(i=0;i<b;i++)

	{

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

		{

			visited[i][j] = false;

			cin >> matrix[i][j];

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

			{

				posisi = make_pair(i,j);

				temp = make_pair(posisi,0);

				antri.push(temp);

			}

		}

	}

	while(!antri.empty())

	{

		sekarang = antri.front();

		antri.pop();

		if(!visited[sekarang.first.first][sekarang.first.second])

		{

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

			angka[sekarang.first.first][sekarang.first.second] = sekarang.second;

			if(sekarang.first.first+1 < b)

			{

				posisi = make_pair(sekarang.first.first+1,sekarang.first.second);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.first-1 >= 0)

			{

				posisi = make_pair(sekarang.first.first-1,sekarang.first.second);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.second+1 < k)

			{

				posisi = make_pair(sekarang.first.first,sekarang.first.second+1);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.second-1 >= 0)

			{

				posisi = make_pair(sekarang.first.first,sekarang.first.second-1);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

		}

	}

	maks = 0;

	for(i=0;i<b;i++)

	{

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

		{

			if(maks < angka[i][j])

			{

				maks = angka[i][j];

			}

		}

	}

	cout << maks << endl;

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



using namespace std;



bool visited[1010][1010];

int angka[1010][1010];

char matrix[1010][1010];

long long maks,b,k,i,j;

pair<pair<int,int>,int> temp,sekarang;

pair<int,int> posisi;

queue <pair<pair<int,int>,int> > antri;

int main()

{

	cin >> b >> k;

	for(i=0;i<b;i++)

	{

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

		{

			visited[i][j] = false;

			cin >> matrix[i][j];

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

			{

				posisi = make_pair(i,j);

				temp = make_pair(posisi,0);

				antri.push(temp);

			}

		}

	}

	while(!antri.empty())

	{

		sekarang = antri.front();

		antri.pop();

		if(!visited[sekarang.first.first][sekarang.first.second])

		{

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

			angka[sekarang.first.first][sekarang.first.second] = sekarang.second;

			if(sekarang.first.first+1 < b)

			{

				posisi = make_pair(sekarang.first.first+1,sekarang.first.second);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.first-1 >= 0)

			{

				posisi = make_pair(sekarang.first.first-1,sekarang.first.second);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.second+1 < k)

			{

				posisi = make_pair(sekarang.first.first,sekarang.first.second+1);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

			if(sekarang.first.second-1 >= 0)

			{

				posisi = make_pair(sekarang.first.first,sekarang.first.second-1);

				temp = make_pair(posisi,sekarang.second+1);

				antri.push(temp);

			}

		}

	}

	maks = 0;

	for(i=0;i<b;i++)

	{

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

		{

			if(maks < angka[i][j])

			{

				maks = angka[i][j];

			}

		}

	}

	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.082020343080144x):
#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;

}