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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

#include <algorithm>

#include <string>

#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <cmath>

#include <queue>

#include <iomanip>



using namespace std;

typedef long long ll;



int  A[1005][1005];



struct node {

	int i;

	int j;

	int step;

}find1[1000001];;

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

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

int k = 1;



int main() {

	int H, W;

	cin >> H >> W;

	char c;

	

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

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

			cin >> c;

			if (c == '#') { A[i][j] = 1; find1[k].i = i; find1[k].j = j; find1[k].step = 0; k++; }

			if (c == '.') { A[i][j] = 0; }



		}

	}

	int p = 1;

	int ans = 0;

	k --;

	while (p<=k) {

		ans = max(ans, find1[p].step);

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

			int x = movex[i] + find1[p].i;

			int y = movey[i] + find1[p].j;

			if (x >= 1 && x <= H && y >= 1 && y <= W && A[x][y] == 0) {

				A[x][y] = 1;

				k++;

				find1[k].i = x;

				find1[k].j = y;

				find1[k].step = find1[p].step + 1;

			}

		}

		p++;

	}

	cout << ans << endl;

	return 0;

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

#define ll long long

using namespace std;

char a[1050][1050],c;

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

struct node{

	int x,y,st;

	node(int _x,int _y,int _z){

		x = _x;y = _y;st = _z;

	}

	node(){

	};

};

queue<node> q;

ll h,w;

int main(){

	ios::sync_with_stdio(false);

	cin >> h >> w;

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

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

			cin >>c;

			a[i][j] = c;

			if(a[i][j] == '#') q.push(node(i,j,1));

		}

	}

	node p;

	while(!q.empty()){

		p = q.front();

		q.pop();

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

			if(a[p.x+mx[i]][p.y+my[i]] == '.'){

				q.push(node(p.x+mx[i],p.y+my[i],p.st+1));

				a[p.x+mx[i]][p.y+my[i]] = '#';

			}		

	}



	cout << p.st-1 << endl;

    return 0;

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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



using namespace std;

typedef long long ll;

char p[1500][1500];

ll n[1500][1500];

ll o[1500],q[1500];

ll z[4]={0,0,1,-1},l[4]={1,-1,0,0};

ll a,b;

struct cha

{

     ll x;

     ll y;

};

queue <cha> m;

void bfs()

{

    while(!m.empty())

    {

       cha top =m.front();

         m.pop();



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

{

    if(top.x+z[i]>=1&&top.x+z[i]<=a&&top.y+l[i]>=1&&top.y+l[i]<=b&&n[top.x+z[i]][top.y+l[i]]==0)

    {

        n[top.x+z[i]][top.y+l[i]]=n[top.x][top.y]+1;



        cha next ;

        next.x=top.x+z[i];

        next.y=top.y+l[i];

        m.push(next);



    }



}



    }







}

int main()

{

   ll c,d,e=0,f=0;

   cin>>a>>b;

   for(c=1;c<=a;c++)

   {

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

   }

   for(c=1;c<=a;c++)

   {



       for(d=1;d<=b;d++)

       {





           if(p[c][d]=='#'){



             cha t;

             t.x=c;

             t.y=d;

             m.push(t);

             n[c][d]=1;









           }



       }



   }





   bfs();

ll flag =-999999;

      for(c=1;c<=a;c++)

   {



       for(d=1;d<=b;d++)

       {



flag=max(flag,n[c][d]);





       }





   }





cout<<flag-1;

    return 0;

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

using namespace std;



int R, C;

int dist[1005][1005], ans;

int sm = 0x3f3f3f3f, bg;



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

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



int main() {

  ios_base::sync_with_stdio(0);

  cin >> R >> C;

  queue<pair<int, int>> q;

  char tmp;

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

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

      cin >> tmp;

      if(tmp == '#'){

        dist[i][j] = 0;

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

      }else dist[i][j] = 0x3f3f3f;

    }

  }



  while(!q.empty()){

    int ux = q.front().first;

    int uy = q.front().second;

    q.pop();



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

      int vx = ux + dx[i];

      int vy = uy + dy[i];

      if(vx >= 0 && vx < R && vy >= 0 && vy < C && dist[vx][vy] > dist[ux][uy] + 1){

        q.push(make_pair(vx, vy));

        dist[vx][vy] = dist[ux][uy] + 1;

        ans = max(ans, dist[vx][vy]);

      }



    }

  }

  cout << ans << "\n";

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

#define fi first

#define se second

#define pb push_back

#define eb emplace_back

using namespace std;

typedef long long LL;

typedef pair<int,int> pii;

typedef vector<int> vi;



template<class T> inline T re(){

	T N = 0; char c = getchar(); bool neg = 0;

	for (; c < '0' || c > '9'; c = getchar()) neg |= c == '-';

	for (; c >= '0' && c <= '9'; c = getchar())

		N = (N<<3)+(N<<1) + c - '0';

	return neg ? -N : N;

}



int h, w;

char g[1005][1005];

int d[1005][1005];

queue<pii> q;



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

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

inline bool ins(int r, int c){

	return 1 <= r && r <= h && 1 <= c && c <= w;

}

int main(){

	scanf("%d %d", &h, &w);

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

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

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

		d[i][j] = 5000 * (g[i][j] == '.' ? 1 : (q.emplace(i,j), 0));

	while (!q.empty()){

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

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

			int r = u.fi + dr[i], c = u.se + dc[i];

			if (ins(r, c) && d[r][c] > d[u.fi][u.se] + 1){

				d[r][c] = d[u.fi][u.se] + 1;

				q.emplace(r, c);

			}

		}

	}

	int mx = 0;

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

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

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

	return 0;

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

#include <queue>

#define pll pair<ll,ll>

#define NMAX 1000006

#define eps 1e-12



using namespace std;

typedef long long ll;

typedef long double ld;



char a[1005][1005];

ll val[1005][1005];

queue<pll> q;

ll n,m;

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

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

bool inside(int x,int y)

{

    if(x<1 || y<1 || x>n || y>m) return 0;

    return 1;

}

int main()

{

    ll i,j,ans=0;

    cin>>n>>m;

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

    {

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

        {

            cin>>a[i][j];

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

            {

                q.push({i,j});

                val[i][j]=0;

            }

        }

    }

    ans=0;

    while(!q.empty())

    {

        i=q.front().first;

        j=q.front().second;

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

        q.pop();

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

        {

            int x=i+dx[d];

            int y=j+dy[d];

            if(inside(x,y) && a[x][y]!='#' && val[x][y]==0)

            {

                val[x][y]=val[i][j]+1;

                q.push({x,y});

            }

        }

    }

    cout<<ans<<'\n';

    return 0;

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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



using namespace std;



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

pair<int,int> Q[1000005];

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

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

int maxim = 1, in, sf;



void bordare()

{

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

        a[i][0] = a[i][m+1] = 1;

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

        a[0][i] = a[n+1][i] = 1;

}



void lee();



int main()

{

    int i, j;

    char c;



    cin >> n >> m;

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

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

        {

            cin >> c;

            if (c == '#')

                a[i][j] = 1;

        }

    bordare();

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

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

            if (a[i][j] == 1)

                Q[sf++] = {i,j};

    lee();

    cout << maxim-1;

    return 0;

}



void lee()

{

    while (in < sf)

    {

        auto aux = Q[in++];

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

        {

            int l9 = aux.first+dx[i];

            int c9 = aux.second+dy[i];

            if (!a[l9][c9])

            {

                a[l9][c9] = a[aux.first][aux.second]+1;

                Q[sf++] = {l9,c9};

                if (a[l9][c9] > maxim)

                    maxim = a[l9][c9];

            }

        }

    }

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



using namespace std;

int n, m;

char op[1005][1005];

bool vis[1005][1005];

int dp[1005][1005];

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



struct Node {

    int x, y;



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

};



queue<Node> que;



int main() {

    ios::sync_with_stdio(false), memset(dp, 127, sizeof(dp));

    cin >> n >> m;

    for (int i = 1; i <= n; i++)cin >> (op[i] + 1);

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

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

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

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

                    int xx = i + X[p], yy = j + Y[p];

                    if (op[xx][yy] == '.' && !vis[xx][yy])vis[xx][yy] = true, dp[xx][yy] = 1, que.push(Node(xx, yy));

                }

            }

        }

    }

    while (!que.empty()) {

        Node s = que.front();

        que.pop(), vis[s.x][s.y] = false;

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

            int xx = s.x + X[i], yy = s.y + Y[i];

            if (xx < 1 || xx > n || yy < 1 || yy > m)continue;

            if (op[xx][yy] == '.' && dp[xx][yy] > dp[s.x][s.y] + 1) {

                dp[xx][yy] = dp[s.x][s.y] + 1;

                if (!vis[xx][yy])que.push(Node(xx, yy)), vis[xx][yy] = true;

            }

        }

    }

    int ans = 0;

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

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

            if (op[i][j] == '.')ans = max(ans, dp[i][j]);

        }

    }

    cout << ans;

    return 0;

}

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

#define Maxn 1007

using namespace std;

int n,m;

char a[Maxn][Maxn];

bool vis[Maxn][Maxn];

int ans=0,dis[Maxn][Maxn];

int dx[4],dy[4];

pair<int,int> que[Maxn*Maxn*2];

int main()

{

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

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

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

    memset(vis,false,sizeof(vis));

    int lx=0,rx=0;

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

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

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

            {

                vis[i][j]=true;

                dis[i][j]=0;

                que[++rx]=make_pair(i,j);

            }

    dx[0]=0,dy[0]=1;

    dx[1]=0,dy[1]=-1;

    dx[2]=1,dy[2]=0;

    dx[3]=-1,dy[3]=0;

    while (lx<rx)

    {

        ++lx;

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

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

        {

            int xx=x+dx[i];

            int yy=y+dy[i];

            if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy]))

            {

                vis[xx][yy]=true;

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

                ans=max(ans,dis[xx][yy]);

                que[++rx]=make_pair(xx,yy);

            }

        }

    }

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

    return 0;

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

using namespace std;

using ii = pair<int, int>;

using iii = pair<ii, int>;

#define mp make_pair

#define fi first

#define se second

const int N = 1e3 + 5;

const int INF = INT_MAX;



int n, m, x, y, z, ans;

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

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

int val[N][N];

char cell[N][N];

iii a;

queue<iii> q;



bool inside(int i) {

    return (x + dx[i] >= 1 && x + dx[i] <= n &&

            y + dy[i] >= 1 && y + dy[i] <= m);

}



void BFS() {

    while(!q.empty()) {

        a = q.front();

        x = a.fi.fi, y = a.fi.se, z = a.se;

        q.pop();

        if(val[x][y] > -1) continue;

        val[x][y] = z;

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

            if(!inside(i)) continue;

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

        }

    }

}



void solve() {

    cin >> n >> m;

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

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

            cin >> cell[i][j];

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

        }

    }

    BFS();

    ans = -INF;

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

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

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

        }

    }

    cout << ans << "\n";

}



int main() {



    ios_base::sync_with_stdio(false);

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



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

    solve();



}

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

#define Maxn 1007

using namespace std;

int n,m;

char a[Maxn][Maxn];

bool vis[Maxn][Maxn];

int ans=0,dis[Maxn][Maxn];

int dx[4],dy[4];

pair<int,int> que[Maxn*Maxn*2];

int main()

{

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

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

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

    memset(vis,false,sizeof(vis));

    int lx=0,rx=0;

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

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

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

            {

                vis[i][j]=true;

                dis[i][j]=0;

                que[++rx]=make_pair(i,j);

            }

    dx[0]=0,dy[0]=1;

    dx[1]=0,dy[1]=-1;

    dx[2]=1,dy[2]=0;

    dx[3]=-1,dy[3]=0;

    while (lx<rx)

    {

        ++lx;

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

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

        {

            int xx=x+dx[i];

            int yy=y+dy[i];

            if (xx>=1&&xx<=n&&yy>=1&&yy<=m&&(!vis[xx][yy]))

            {

                vis[xx][yy]=true;

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

                ans=max(ans,dis[xx][yy]);

                que[++rx]=make_pair(xx,yy);

            }

        }

    }

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

    return 0;

}