stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

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

#define lld long long

#define nmax 1005

#define pii pair<int,int>

#define pb push_back

using namespace std;



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

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

int ans, n, m;

queue<pii>q, q2;

bool st[nmax][nmax];

char a[nmax][nmax];



bool interior(int i, int j){

    return i >= 1 && j >= 1 && i <= n && j <= m;

}



int main() {

    cin>>n>>m;

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

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

            cin>>a[i][j];

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

                st[i][j] = true;

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

            }

        }

    }

    while (!q.empty()){

        ++ans;

        while (!q2.empty()){q2.pop();}

        while (!q.empty()){

            auto act = q.front();

            int i = act.first, j = act.second;

            q.pop();

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

                if (!interior(i+dl[k],j+dc[k])){

                    continue;

                }

                if (st[i+dl[k]][j+dc[k]]){

                    continue;

                }

                if (a[i+dl[k]][j+dc[k]] == '#'){

                    continue;

                }

                st[i+dl[k]][j+dc[k]] = true;

                q2.push(make_pair(i+dl[k],j+dc[k]));

            }

        }

        q = q2;

    }

    --ans;

    if (ans < 0){

        ans = 0;

    }

    cout<<ans<<'\n';

    return 0;

}
// Slower C++ solution:
#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;

}

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



using namespace std;



const short Nmax = 1005;



char a[Nmax][Nmax];





int n , m , dist[Nmax][Nmax];



queue < pair < int , int > > Q;



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

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



int main()

{

    int mx = 0;

    cin >> n >> m;

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

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

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

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

            dist[i][j] = 1e9;

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

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

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

                    dist[i][j] = 0 , Q.push({i , j});



    while(!Q.empty())

    {

        int x = Q.front().first;

        int y = Q.front().second;

        Q.pop();

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

        {

            int xx = x + dx[k];

            int yy = y + dy[k];

            if(!(1 <= xx && xx <= n && 1 <= yy && yy <= m))

                    continue;

            if(dist[xx][yy] > dist[x][y] + 1)

            {

                dist[xx][yy] = dist[x][y] + 1;

                Q.push({xx , yy});

            }

        }

    }

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

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

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

    cout << mx << "\n";

}
// Slower C++ solution:
#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;

}

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

#include<cstring>

#include<string>

#include<cmath>

#include<queue>

#include<map>

using namespace std;





int distanta(pair<int,int> a,pair<int,int> b);



int main()

{

    int R,C;

    cin>>R>>C;

    string harta[1001];

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

        cin>>harta[i];

    }



    queue<pair<int,int>> coada;

    int adaugateUltimaData=0;



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

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

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

                coada.push({i,j});

                adaugateUltimaData++;

            }

        }

    }



    int ture=0;

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

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

    while(!coada.empty()){

        ture++;

        int adaugateAcum=0;

        while(adaugateUltimaData){

            pair<int,int> coords=coada.front();

            coada.pop();

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

                int newI=coords.first+dl[k];

                int newJ=coords.second+dc[k];

                if(newI>=1&&newI<=R&&newJ>=0&&newJ<C){

                    if(harta[newI][newJ]!='#'){

                        harta[newI][newJ]='#';

                        coada.push({newI,newJ});

                        adaugateAcum++;

                    }

                }

            }



            adaugateUltimaData--;

        }



        adaugateUltimaData=adaugateAcum;

    }



    cout<<ture-1;







}



int distanta(pair<int,int> a,pair<int,int> b){

    return abs(a.first-b.first)+abs(a.second-b.second);

}
// Slower C++ solution:
#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;

}

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

using namespace std;

#define mp make_pair

#define fi first

#define se second

int n , m , maxi , dx[5] , dy[5];

char grid[2000][2000];

bool cek[2000][2000];

typedef pair<pair<int , int>,int> iii;

iii a;

queue<iii>q;

int main(){

	cin >> n >> m;

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

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

			cin >> grid[i][j];

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

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

			}

		}

	}

	dx[1] = 1;

	dx[2] = -1;

	dx[3] = 0;

	dx[4] = 0;

	dy[1] = 0;

	dy[2] = 0;

	dy[3] = 1;

	dy[4] = -1;

	maxi = 0;

	while(!q.empty()){

		a = q.front();

		int x = a.fi.fi;

		int y = a.fi.se;

		int z = a.se;

		maxi = max(maxi , z);

		q.pop();

		if(cek[x][y])continue;

		cek[x][y] = true;

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

			if(x + dx[i] < 1 || x + dx[i] > n || y + dy[i] < 1  || y + dy[i] > m)continue;

			q.push(mp(mp(x + dx[i], y + dy[i]) , z + 1));

		}

	}

	maxi -= 1;

	if(maxi < 0)cout << 0 << endl;

	else cout << maxi << endl;

}
// Slower C++ solution:
#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';

}

// Faster C++ solution for the same problem (measured speedup: 1.1225628316602891x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.215144735216062x):
#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;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.197476942110627x):
#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;

}
// Slower C++ solution:
#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';

}

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



const int MAX_N = int(1e3) + 10;



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

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



int n, m;

int dis[MAX_N][MAX_N];

bool vis[MAX_N][MAX_N];

char s[MAX_N];

std::vector<std::pair<int, int> > que;



int main() {

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

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

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

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

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

				que.emplace_back(i, j);

				vis[i][j] = 1;

				dis[i][j] = 0;

			}

		}

	}



	int ans = 0;

	for (int i = 0; i < que.size(); ++i) {

		int x = que[i].first, y = que[i].second;

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

			int _x = x + dx[k], _y = y + dy[k];

			if (_x >= 1 && _x <= n && _y >= 1 && _y <= m && !vis[_x][_y]) {

				vis[_x][_y] = 1;

				dis[_x][_y] = dis[x][y] + 1;

				que.emplace_back(_x, _y);

				if (dis[_x][_y] > ans) ans = dis[_x][_y];

			}

		}

	}

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

}
// Slower C++ solution:
#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;

}

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



const int MAX_N = int(1e3) + 10;



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

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



int n, m;

int dis[MAX_N][MAX_N];

bool vis[MAX_N][MAX_N];

char s[MAX_N];

std::vector<std::pair<int, int> > que;



int main() {

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

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

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

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

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

				que.emplace_back(i, j);

				vis[i][j] = 1;

				dis[i][j] = 0;

			}

		}

	}



	int ans = 0;

	for (int i = 0; i < que.size(); ++i) {

		int x = que[i].first, y = que[i].second;

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

			int _x = x + dx[k], _y = y + dy[k];

			if (_x >= 1 && _x <= n && _y >= 1 && _y <= m && !vis[_x][_y]) {

				vis[_x][_y] = 1;

				dis[_x][_y] = dis[x][y] + 1;

				que.emplace_back(_x, _y);

				if (dis[_x][_y] > ans) ans = dis[_x][_y];

			}

		}

	}

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

}
// Slower C++ solution:
#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;

}

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



const int MAX_N = int(1e3) + 10;



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

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



int n, m;

int dis[MAX_N][MAX_N];

bool vis[MAX_N][MAX_N];

char s[MAX_N];

std::vector<std::pair<int, int> > que;



int main() {

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

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

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

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

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

				que.emplace_back(i, j);

				vis[i][j] = 1;

				dis[i][j] = 0;

			}

		}

	}



	int ans = 0;

	for (int i = 0; i < que.size(); ++i) {

		int x = que[i].first, y = que[i].second;

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

			int _x = x + dx[k], _y = y + dy[k];

			if (_x >= 1 && _x <= n && _y >= 1 && _y <= m && !vis[_x][_y]) {

				vis[_x][_y] = 1;

				dis[_x][_y] = dis[x][y] + 1;

				que.emplace_back(_x, _y);

				if (dis[_x][_y] > ans) ans = dis[_x][_y];

			}

		}

	}

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

}