stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<iostream>

#include<queue>

#include<cstring>



using namespace std;



queue<pair<int, int>> q;

int H, W;

int grid[1005][1005], shortestPath[1005][1005];

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

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



bool inside(int row, int col){

  if (row < 0 || row >= H || col < 0 || col >= W) return false;

  return true;

}



int bfs(){

  int ans = 0;

  while(!q.empty()){

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

    q.pop();



    int row = now.first;

    int col = now.second;



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



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

      int nextRow = row + dx[i];

      int nextCol = col + dy[i];



      if(inside(nextRow, nextCol) && grid[nextRow][nextCol] != 1 && shortestPath[nextRow][nextCol] == -1){

        shortestPath[nextRow][nextCol] = 1 + shortestPath[row][col];

        q.push(make_pair(nextRow, nextCol));

      }

    }



  }

  return ans;

}



int main(){

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

  cin >> H >> W;



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

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

      char c;

      cin >> c;

      if(c == '#'){

        grid[i][j] = 1;

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

        shortestPath[i][j] = 0;

      }

    }

  }



  cout << bfs() << endl;



}

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

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

 

using namespace std;

 

const int N = 1e3+5;

 

int n,m,ans;

 

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

 

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

 

char e[N][N];

 

struct node{

 

    int x;

 

    int y;

 

    int step;

 

};

 

queue<node>Q;

 

 

 

void bfs(){

 

    while(!Q.empty()){

 

        node a=Q.front();

 

        Q.pop();

 

        ans=max(ans,a.step);

 

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

		{

 

            node b;

 

            int xx=a.x+dx[i];

 

            int yy=a.y+dy[i];

 

            if(xx<0||xx>=n||yy<0||yy>=m)

				continue;

 

            b.x=xx;

 

            b.y=yy;

 

            b.step=a.step+1;

 

            if(e[xx][yy]=='.')

			{

                e[xx][yy]='#';

                Q.push(b);

            }

 

        }

 

    }

}

 

int main()

 

{

 

    std::ios::sync_with_stdio(false);

 	cin.tie(0);

    cin>>n>>m;

 

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

	{

        cin>>e[i];

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

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

				Q.push(node{i,j,0});

    }

 

    bfs();

 

    cout<<ans<<endl;

 

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

 

using namespace std;

 

const int N = 1e3+5;

 

int n,m,ans;

 

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

 

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

 

char e[N][N];

 

struct node{

 

    int x;

 

    int y;

 

    int step;

 

};

 

queue<node>Q;

 

 

 

void bfs(){

 

    while(!Q.empty()){

 

        node a=Q.front();

 

        Q.pop();

 

        ans=max(ans,a.step);

 

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

		{

 

            node b;

 

            int xx=a.x+dx[i];

 

            int yy=a.y+dy[i];

 

            if(xx<0||xx>=n||yy<0||yy>=m)

				continue;

 

            b.x=xx;

 

            b.y=yy;

 

            b.step=a.step+1;

 

            if(e[xx][yy]=='.')

			{

                e[xx][yy]='#';

                Q.push(b);

            }

 

        }

 

    }

}

 

int main()

 

{

 

    std::ios::sync_with_stdio(false);

 	cin.tie(0);

    cin>>n>>m;

 

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

	{

        cin>>e[i];

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

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

				Q.push(node{i,j,0});

    }

 

    bfs();

 

    cout<<ans<<endl;

 

    return 0;

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

#include<queue>

#include<cstring>



using namespace std;



queue<pair<int, int>> q;

int H, W;

int grid[1005][1005], shortestPath[1005][1005];

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

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



bool inside(int row, int col){

  if (row < 0 || row >= H || col < 0 || col >= W) return false;

  return true;

}



int bfs(){

  int ans = 0;

  while(!q.empty()){

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

    q.pop();



    int row = now.first;

    int col = now.second;



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



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

      int nextRow = row + dx[i];

      int nextCol = col + dy[i];



      if(inside(nextRow, nextCol) && grid[nextRow][nextCol] != 1 && shortestPath[nextRow][nextCol] == -1){

        shortestPath[nextRow][nextCol] = 1 + shortestPath[row][col];

        q.push(make_pair(nextRow, nextCol));

      }

    }



  }

  return ans;

}



int main(){

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

  cin >> H >> W;



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

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

      char c;

      cin >> c;

      if(c == '#'){

        grid[i][j] = 1;

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

        shortestPath[i][j] = 0;

      }

    }

  }



  cout << bfs() << endl;



}

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

 

using namespace std;

 

const int N = 1e3+5;

 

int n,m,ans;

 

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

 

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

 

char e[N][N];

 

struct node{

 

    int x;

 

    int y;

 

    int step;

 

};

 

queue<node>Q;

 

 

 

void bfs(){

 

    while(!Q.empty()){

 

        node a=Q.front();

 

        Q.pop();

 

        ans=max(ans,a.step);

 

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

		{

 

            node b;

 

            int xx=a.x+dx[i];

 

            int yy=a.y+dy[i];

 

            if(xx<0||xx>=n||yy<0||yy>=m)

				continue;

 

            b.x=xx;

 

            b.y=yy;

 

            b.step=a.step+1;

 

            if(e[xx][yy]=='.')

			{

                e[xx][yy]='#';

                Q.push(b);

            }

 

        }

 

    }

}

 

int main()

 

{

 

    std::ios::sync_with_stdio(false);

 	cin.tie(0);

    cin>>n>>m;

 

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

	{

        cin>>e[i];

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

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

				Q.push(node{i,j,0});

    }

 

    bfs();

 

    cout<<ans<<endl;

 

    return 0;

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

}

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

 

using namespace std;

 

const int N = 1e3+5;

 

int n,m,ans;

 

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

 

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

 

char e[N][N];

 

struct node{

 

    int x;

 

    int y;

 

    int step;

 

};

 

queue<node>Q;

 

 

 

void bfs(){

 

    while(!Q.empty()){

 

        node a=Q.front();

 

        Q.pop();

 

        ans=max(ans,a.step);

 

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

		{

 

            node b;

 

            int xx=a.x+dx[i];

 

            int yy=a.y+dy[i];

 

            if(xx<0||xx>=n||yy<0||yy>=m)

				continue;

 

            b.x=xx;

 

            b.y=yy;

 

            b.step=a.step+1;

 

            if(e[xx][yy]=='.')

			{

                e[xx][yy]='#';

                Q.push(b);

            }

 

        }

 

    }

}

 

int main()

 

{

 

    std::ios::sync_with_stdio(false);

 	cin.tie(0);

    cin>>n>>m;

 

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

	{

        cin>>e[i];

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

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

				Q.push(node{i,j,0});

    }

 

    bfs();

 

    cout<<ans<<endl;

 

    return 0;

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

#include <cstdio>

#include <queue>

#include <cstring>

using namespace std;



//char s[1003][1003];

int vis[1003][1003];

struct node

{

    int x;int y;

};



int main()

{

    int h,w;

    cin>>h>>w;

    queue<node> q;

    memset(vis,0,sizeof(vis));

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

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

            char s;cin>>s;

            if(s=='#'){

                node a;a.x=i;a.y=j;

                q.push(a);

                vis[i][j]=1;

            }

        }

    }

    while(!q.empty()){

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

        if(a.x>0&&!vis[a.x-1][a.y]){

            vis[a.x-1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x-1;b.y=a.y;

            q.push(b);

        }

        if(a.y>0&&!vis[a.x][a.y-1]){

            vis[a.x][a.y-1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y-1;

            q.push(b);

        }

        if(a.x<h-1&&!vis[a.x+1][a.y]){

            vis[a.x+1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x+1;b.y=a.y;

            q.push(b);

        }

        if(a.y<w-1&&!vis[a.x][a.y+1]){

            vis[a.x][a.y+1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y+1;

            q.push(b);

        }

    }

    int ans=0;

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

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

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

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

        }

        //cout<<endl;

    }

    cout<<ans-1<<endl;

    return 0;

}


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

#include <cstdio>

#include <cstring>

#include <cmath>

#include <algorithm>

#include <map>

#include <vector>

#include <set>

#include <queue>

#include <cstdlib>



using namespace std;



#define LL long long

#define INF 0x7fffffff

#define P 998244353



inline LL read(){

    LL x=0,f=1; char ch=getchar();

    while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}

    while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}

    return x*f;

}



struct Node{

	int x,y,d;

	Node (){}

	Node (int X,int Y,int D){

		x=X,y=Y,d=D;

	}

};



int visit[1010][1010],ans,dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};

char mp[1010][1010];

int H,W,Sx,Sy;



inline bool check(int x,int y){

	if (x<1 || x>H) return 0;

	if (y<1 || y>W) return 0;

	if (visit[x][y]) return 0;

	return 1;

}

queue<Node>q;

inline void bfs(){

	

	while (!q.empty()){

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

		// printf("debug %d %d %d\n",now.x,now.y,now.d);

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

			int tx = now.x+dx[i], ty = now.y+dy[i], td = now.d+1;

			// printf("%d %d %d\n",tx,ty,td);

			if (!check(tx,ty)) continue;

			visit[tx][ty]=1;

			q.push(Node(tx,ty,td));

			

			ans = max(ans,td);

		}

	}

}



int main(){

	H=read(),W=read();

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

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

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

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

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

				visit[i][j]=1;

			}

		}

	}

	bfs();

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

	return 0;

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

#include <cstdio>

#include <queue>

#include <cstring>

using namespace std;



//char s[1003][1003];

int vis[1003][1003];

struct node

{

    int x;int y;

};



int main()

{

    int h,w;

    cin>>h>>w;

    queue<node> q;

    memset(vis,0,sizeof(vis));

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

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

            char s;cin>>s;

            if(s=='#'){

                node a;a.x=i;a.y=j;

                q.push(a);

                vis[i][j]=1;

            }

        }

    }

    while(!q.empty()){

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

        if(a.x>0&&!vis[a.x-1][a.y]){

            vis[a.x-1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x-1;b.y=a.y;

            q.push(b);

        }

        if(a.y>0&&!vis[a.x][a.y-1]){

            vis[a.x][a.y-1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y-1;

            q.push(b);

        }

        if(a.x<h-1&&!vis[a.x+1][a.y]){

            vis[a.x+1][a.y]=vis[a.x][a.y]+1;

            node b;b.x=a.x+1;b.y=a.y;

            q.push(b);

        }

        if(a.y<w-1&&!vis[a.x][a.y+1]){

            vis[a.x][a.y+1]=vis[a.x][a.y]+1;

            node b;b.x=a.x;b.y=a.y+1;

            q.push(b);

        }

    }

    int ans=0;

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

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

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

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

        }

        //cout<<endl;

    }

    cout<<ans-1<<endl;

    return 0;

}


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

using namespace std;

#define ll long long

#define mod 998244353

#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int inf = 0x3f3f3f3f;

const ll INF = 0x3f3f3f3f3f3f3f3f;

const int maxn  = 1e5+5;

typedef struct node{

    int x, y;

}node;

node a[1000005];

int top,topp,h,w,cnt,toppp;

int vis[1005][1005];

char m[1005][1005];

void bfs(){

    topp=top; toppp=1;

    while(top<h*w){

        for(int i=toppp;i<=top;i++){

            if(!vis[a[i].x+1][a[i].y] && a[i].x+1<=h){

                vis[a[i].x+1][a[i].y]=1;

                a[++topp]=node{a[i].x+1,a[i].y};

            }

            if(!vis[a[i].x-1][a[i].y] && a[i].x-1>=1) {

                vis[a[i].x-1][a[i].y]=1;

                a[++topp]=node{a[i].x-1,a[i].y};

            }

            if(!vis[a[i].x][a[i].y+1] && a[i].y+1<=w){

                vis[a[i].x][a[i].y+1]=1;

                a[++topp]=node{a[i].x,a[i].y+1};

            }

            if(!vis[a[i].x][a[i].y-1] && a[i].y-1>=1){

                vis[a[i].x][a[i].y-1]=1;

                a[++topp]=node{a[i].x,a[i].y-1};

            }

        }

        toppp=top;

        top=topp;

        cnt++;

    }

    cout << cnt << endl;

}

int main(){

    cin >> h >> w;

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

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

            cin >> m[i][j];

            if(m[i][j]=='#') a[++top]=node{i,j},vis[i][j]=1;

        }

    }

    bfs();

    return 0;

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

#include <queue>

#include <cstring>

const int maxn=1010;

using namespace std;

struct Node {

	int x,y;

};

queue <Node> Q;

int a[maxn][maxn],d[maxn][maxn];

int vis[maxn][maxn];

int main(){

	memset(vis,0,sizeof(vis));

	memset(d,0,sizeof(d));

	ios::sync_with_stdio(false);

	cin.tie(0);

	int h,w;

	cin >> h>>w;

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

		string s;

		cin >> s;

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

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

				a[i][j+1] = 1;

				Q.push((Node){i,j+1});

				vis[i][j+1] = 1;

			}else a[i][j+1] = 0;

			// a[i][j+1] = s[j]=='h'?1:0;



		}

	}



	int cnt = 0;

	while (!Q.empty()){

		Node p=Q.front();

		Q.pop();

		if (p.x>1 && !vis[p.x-1][p.y]){

			d[p.x-1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x-1,p.y});

			vis[p.x-1][p.y] = 1;

		}

		if (p.y>1 && !vis[p.x][p.y-1]){

			d[p.x][p.y-1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y-1});

			vis[p.x][p.y-1] = 1;

		}

		if (p.x<h && !vis[p.x+1][p.y]){

			d[p.x+1][p.y] = d[p.x][p.y] + 1;

			Q.push((Node){p.x+1,p.y});

			vis[p.x+1][p.y] = 1;

		}

		if (p.y<w && !vis[p.x][p.y+1]){

			d[p.x][p.y+1] = d[p.x][p.y] + 1;

			Q.push((Node){p.x,p.y+1});

			vis[p.x][p.y+1] = 1;

		}

	}

	int ans=0;

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

	// 	for (int j=1;j<=w;j++) ans=max(ans,d[i][j]),cout << d[i][j]<<' ';

	// 	cout << endl;

	// }

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

		for (int j=1;j<=w;j++) ans=max(ans,d[i][j]);

	

	cout << ans;

}

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

using namespace std;

#define ll long long

#define mod 998244353

#define IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)

const int inf = 0x3f3f3f3f;

const ll INF = 0x3f3f3f3f3f3f3f3f;

const int maxn  = 1e5+5;

typedef struct node{

    int x, y;

}node;

node a[1000005];

int top,topp,h,w,cnt,toppp;

int vis[1005][1005];

char m[1005][1005];

void bfs(){

    topp=top; toppp=1;

    while(top<h*w){

        for(int i=toppp;i<=top;i++){

            if(!vis[a[i].x+1][a[i].y] && a[i].x+1<=h){

                vis[a[i].x+1][a[i].y]=1;

                a[++topp]=node{a[i].x+1,a[i].y};

            }

            if(!vis[a[i].x-1][a[i].y] && a[i].x-1>=1) {

                vis[a[i].x-1][a[i].y]=1;

                a[++topp]=node{a[i].x-1,a[i].y};

            }

            if(!vis[a[i].x][a[i].y+1] && a[i].y+1<=w){

                vis[a[i].x][a[i].y+1]=1;

                a[++topp]=node{a[i].x,a[i].y+1};

            }

            if(!vis[a[i].x][a[i].y-1] && a[i].y-1>=1){

                vis[a[i].x][a[i].y-1]=1;

                a[++topp]=node{a[i].x,a[i].y-1};

            }

        }

        toppp=top;

        top=topp;

        cnt++;

    }

    cout << cnt << endl;

}

int main(){

    cin >> h >> w;

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

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

            cin >> m[i][j];

            if(m[i][j]=='#') a[++top]=node{i,j},vis[i][j]=1;

        }

    }

    bfs();

    return 0;

}