stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <iostream>

#include <stdio.h>

#include <iomanip>

#include <climits>

#include <vector>

#include <cstring>

#include <algorithm>

#include <queue>

#define ll long long

#define REP(a,b) for(int a=0; a<b; a++)

using namespace std;



int H, W;

int grid[1005][1005];

queue<pair<int, int>> q;

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

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



bool inside(int r, int c){

return r >= 0 && r < H && c >= 0 && c < W;

}



int main() {

  ios_base::sync_with_stdio(false);

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

  cin >> H >> W;

  REP(i, H){

    string temp;

    cin >> temp;

    REP(j, W){

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

        grid[i][j] = 0;

        q.emplace(i, j);

      }

    }

  }

  while(!q.empty()){

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

    q.pop();

    REP(i, 4){

      int xx = coord.first+mx[i];

      int yy = coord.second+my[i];

      if(inside(xx, yy))if(grid[xx][yy] == -1){

        grid[xx][yy] = grid[coord.first][coord.second]+1;

        q.emplace(xx, yy);

      }

    }

  }

  int mxm = 0;

  REP(i, H){

    REP(j, W){

      if(grid[i][j] > mxm) mxm = grid[i][j];

    }

  }

  cout << mxm << endl;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1000 + 7, maxm = 1e6 + 7;

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

int n, m, vis[maxn][maxn];

char r[maxn];



struct Node{

	int x, y;

}node[maxm];

int s = 1, t = 0;



void bfs() {

	int x, y, X, Y;

	while(s <= t) {

		x = node[s].x; y = node[s].y;

		++s;

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

			X = x + D[i][0];

			Y = y + D[i][1];

			if(~vis[X][Y]) continue;

			vis[X][Y] = vis[x][y] + 1;

			node[++t].x = X; node[t].y = Y;

		}

	}

	printf("%d\n", vis[x][y]);

}



int main() {

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

	for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) vis[i][j] = -1;

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

		scanf("%s", r + 1);

		for (int j = 1; j <= m; ++j) if(r[j] == '#') {

			node[++t].x = i; node[t].y = j;

			vis[i][j] = 0;

		}

	}

	bfs();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int main()

{

	int a[1001][1001],x,y,ans=0;

	char b[1001][1001],c;

	

	priority_queue <pair<int,int> > xy;

	

	cin>>x>>y;

	

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

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

			cin>>c;

			b[i][j]=c;

			if(c=='#') {

				a[i][j] = 0;

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

			}

			else a[i][j] = x + y -1;

		}

	}

		

//	while(!xy.empty()){

//		int xx = xy.top().first;

//		int yy = xy.top().second;

//		//cout<<xx<<yy<<'\n';

//		xy.pop();

//		for(int i=0;i<y;i++){

//			if(a[xx][i]==-1) a[xx][i] = abs(yy-i);

//			else a[xx][i] = min(a[xx][i],abs(yy-i)); 

//		}

//		for(int i =0;i<x;i++){

//			if(a[i][yy]==-1) a[i][yy] = abs(xx-i);

//			else a[i][yy] = min(a[i][yy],abs(xx-i));

//		}

//	}

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

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

//			if(a[i][j]==-1){

//				a[i][j] = x + y -1;

//			}

			if(i>0 && a[i-1][j]!=-1) a[i][j] = min(a[i][j],a[i-1][j]+1);

			if(i<x-1 && a[i+1][j]!=-1) a[i][j] = min(a[i][j],a[i+1][j]+1);

			if(j>0 && a[i][j-1]!=-1) a[i][j] = min(a[i][j],a[i][j-1]+1);

			if(j<y-1 && a[i][j+1]!=-1) a[i][j] = min(a[i][j],a[i][j+1]+1);

			//ans = max(ans,a[i][j]);

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

		}

		//cout<<'\n';

	}

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

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

//			if(a[i][j]==-1){

//				a[i][j] = x + y -1;

//			}

			if(i>0 && a[i-1][j]!=-1) a[i][j] = min(a[i][j],a[i-1][j]+1);

			if(i<x-1 && a[i+1][j]!=-1) a[i][j] = min(a[i][j],a[i+1][j]+1);

			if(j>0 && a[i][j-1]!=-1) a[i][j] = min(a[i][j],a[i][j-1]+1);

			if(j<y-1 && a[i][j+1]!=-1) a[i][j] = min(a[i][j],a[i][j+1]+1);

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

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

		}

		//cout<<'\n';

	}

//	for(int i=0;i<x;i++){

//		for(int j=0;j<y;j++){

//			ans = max(ans,a[i][j]);

//		}

//	}

	cout<<ans<<'\n';

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1000 + 7, maxm = 1e6 + 7;

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

int n, m, vis[maxn][maxn];

char r[maxn];



struct Node{

	int x, y;

}node[maxm];

int s = 1, t = 0;



void bfs() {

	int x, y, X, Y;

	while(s <= t) {

		x = node[s].x; y = node[s].y;

		++s;

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

			X = x + D[i][0];

			Y = y + D[i][1];

			if(~vis[X][Y]) continue;

			vis[X][Y] = vis[x][y] + 1;

			node[++t].x = X; node[t].y = Y;

		}

	}

	printf("%d\n", vis[x][y]);

}



int main() {

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

	for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) vis[i][j] = -1;

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

		scanf("%s", r + 1);

		for (int j = 1; j <= m; ++j) if(r[j] == '#') {

			node[++t].x = i; node[t].y = j;

			vis[i][j] = 0;

		}

	}

	bfs();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <queue>

#include <cstring>

using namespace std;

char grid[1005][1005];

# define pii pair<int,int>



int shortestPath[1005][1005];

int R, C;

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

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



bool inside(int r, int c){

  return 0<=r && r<R && 0<=c && c<C;

}



int bfs(){

  queue<pii> q;

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



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

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

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

        shortestPath[i][j] = 0;

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

      }

    }

  }

  

  int ans = 0;

  while(!q.empty()){

    pii now = q.front(); q.pop();

    int row = now.first;

    int col = now.second;



    ans = max(ans, shortestPath[row][col]);



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

      int nxr = row+dx[d];

      int nxc = col+dy[d];

      if(!inside(nxr, nxc)) continue;

      if(shortestPath[nxr][nxc] != -1) continue;

      shortestPath[nxr][nxc] = shortestPath[row][col] + 1;

      q.push(make_pair(nxr, nxc));

    }

  }



  return ans;

}



int main() {

  cin >> R >> C;

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

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

      cin >> grid[i][j];

    }

  }



  cout << bfs() << endl;

}

// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1000 + 7, maxm = 1e6 + 7;

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

int n, m, vis[maxn][maxn];

char r[maxn];



struct Node{

	int x, y;

}node[maxm];

int s = 1, t = 0;



void bfs() {

	int x, y, X, Y;

	while(s <= t) {

		x = node[s].x; y = node[s].y;

		++s;

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

			X = x + D[i][0];

			Y = y + D[i][1];

			if(~vis[X][Y]) continue;

			vis[X][Y] = vis[x][y] + 1;

			node[++t].x = X; node[t].y = Y;

		}

	}

	printf("%d\n", vis[x][y]);

}



int main() {

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

	for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) vis[i][j] = -1;

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

		scanf("%s", r + 1);

		for (int j = 1; j <= m; ++j) if(r[j] == '#') {

			node[++t].x = i; node[t].y = j;

			vis[i][j] = 0;

		}

	}

	bfs();

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
# 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;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}


// Optimized solution to the same problem (faster):
#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;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#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;

}

// Optimized solution to the same problem (faster):
#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;

}

// Fastest known solution for this problem: