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

#include <string>

using namespace std;

int n;

int a,b;



int main() {

    cin>>n>>a>>b;

    int answer=0;

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

    {

        string temp= to_string(i);

        int count=0;

        for(int t=0;t<temp.length();t++)

        {

            count+=temp[t]-'0';

        }

        if(count>=a&&count<=b)

        answer+=i;

    }

    cout<<answer;

}


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



int main () {

	int ans = 0;

	int N, A, B;

	scanf ("%d %d %d", &N, &A, &B);

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

		int digit = 0;

		int aa = i;

		while (aa != 0) {

			digit = digit + aa % 10;

			aa /= 10;

		}

		

		if (digit >= A && digit <= B) {

			ans += i;

		}

//		if (i == 10 || i == 100 || i == 1000 || i == 10000) {

//			if (1 >= A && 1 <= B) {

//				ans += i;	

//			}

//		} else if (i < 10) {

//			if (i >= A && i <= B) {

//				ans += i;

//				}

//		} else if (i > 10 && i < 100) {

//			int Bb = i / 10;

//			int Bbb = i - (Bb*10);

//			int B1 = Bb + Bbb;

//			if (B1 >= A && B1 <= B) {

//				ans += i;

//			}	

//		} else if (i > 100 && i < 1000) {

//			int Cc = i / 100;

//			int Ccc = (i - (Cc*100)) / 10;

//			int Cccc = i - (Cc * 100) - (Ccc * 10);

//			int C1 = Cc + Ccc + Cccc;

//			if (C1 >= A && C1 <= B) {

//				ans += i;

//			}	

//		} else if (i > 1000 && i < 10000) {

//			int D = i / 1000;

//			int Dd = (i - (D*100)) / 100;

//			int Ddd = (i - (D * 1000) - (Dd * 100) / 10);

//			int Dddd = i - (D * 1000) - (Dd * 100) - (Ddd * 10);

//			int D1 = D + Dd + Ddd + Dddd;

//			if (D1 >= A && D1 <= B) {

//				ans += i;

//			}

//		}

	}

	printf ("%d", ans);

	return 0;

}

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

using namespace std;

int main()



{

    int n,a,b;

    scanf("%d %d %d",&n,&a,&b);

    int i,j,x,y,s=0,hp;

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

    {

        x=0;

        y=i;

        for(j=0;;j++)

        {

            if(pow(10,j)>y)

            {

                hp=j-1;

                break;

            }

        }

        for(j=hp;j>=0;j--)

        {

            int power=pow(10,j);

            x=x+y/power;

            y=y%power;

        }







        if(x>=a&&x<=b)

        {

            s=s+i;

        }

    }

    printf("%d",s);

    return 0;

}


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



int main() {

	int n, a, b;

	scanf ("%d %d %d", &n, &a, &b);

	int sum = 0;

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

		int digitsum = 0;

		int temp = i;

		while (temp != 0){

			digitsum = digitsum + temp % 10;

			temp /= 10;

		}

		if (digitsum >= a && digitsum <= b){

			sum += i;

		}

	}

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

	return 0;

}

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

using namespace std;



int main ()



{

    int a,c,N,A,B,i,x,y,z,k,j;

    j=0;

    scanf("%d%d%d",&N,&A,&B);

    c=N;

    for(i=1;i<=c;i++){

        N=i;

        x=N/10000.0;

        N=N-10000*x;

        y=N/1000.0;

        N=N-1000*y;

        z=N/100.0;

        N=N-z*100;

        a=N/10.0;

        N=N-a*10;



        k=a+z+x+y+N;

        if(k>=A && k<=B){

            j=i+j;

        }





    }

    printf("%d",j);

}


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



int main() {

	int n, a, b;

	scanf ("%d %d %d", &n, &a, &b);

	int sum = 0;

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

		int digitsum = 0;

		int temp = i;

		while (temp != 0){

			digitsum = digitsum + temp % 10;

			temp /= 10;

		}

		if (digitsum >= a && digitsum <= b){

			sum += i;

		}

	}

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

	return 0;

}

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

using namespace std;

#define pb push_back

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

#define rep1(i,n) for(int i=1;i<=n;i++)

#define mp make_pair

#define F first

#define S second

#define IOS ios_base::sync_with_stdio(0); cin.tie(0)

typedef long long ll;

vector<int> g[100005];

ll q,n;



int main()

{

    //IOS;

    while(cin>>n)

    {

        ll sum=0;

        ll A,B;

        cin>>A>>B;

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

        {

            ll a=i/10000,b=(i%10000)/1000,c=(i%1000)/100,d=(i%100)/10,e=i%10;

            if(a+b+c+d+e<=B&&a+b+c+d+e>=A) sum+=i;

        }

        cout<<sum<<"\n";

    }



    return 0;

}




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



int main() {

	int n, a, b;

	scanf ("%d %d %d", &n, &a, &b);

	int sum = 0;

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

		int digitsum = 0;

		int temp = i;

		while (temp != 0){

			digitsum = digitsum + temp % 10;

			temp /= 10;

		}

		if (digitsum >= a && digitsum <= b){

			sum += i;

		}

	}

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

	return 0;

}

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

int main()

{

    int i,j,k,l,m,n,o,a,b,sum;

    scanf("%d",&n);

    scanf("%d",&a);

    scanf("%d",&b);

    sum=0;

    for(o=0;o<=9;o++)

    {

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

        {

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

            {

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

                {

                    for(l=0;l<=9;l++)

                    {

                        for(m=a;m<=b&&10000*o+1000*i+100*j+10*k+l<=n;m++)

                        {

                            if(m==o+i+j+k+l)

                            {

                                sum=sum+10000*o+1000*i+100*j+10*k+l;

                                break;

                            }

                        }

                    }

                }

            }

        }

    }

    printf("%d",sum);

    return 0;

}


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



int main() {

	int n, a, b;

	scanf ("%d %d %d", &n, &a, &b);

	int sum = 0;

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

		int digitsum = 0;

		int temp = i;

		while (temp != 0){

			digitsum = digitsum + temp % 10;

			temp /= 10;

		}

		if (digitsum >= a && digitsum <= b){

			sum += i;

		}

	}

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

	return 0;

}

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



using namespace std;



int main(){

    int n,a,b,ans=0,d,p=0;

    cin >> n >> a >> b;

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

        d=t,p=0;

        while(d>=1){

            p+=d%10;

            d/=10;

        }

        if(p>=a && p<=b){

            ans+=t;

        }

    }

    cout << ans << '\n';

}

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



int main() {

	int n, a, b;

	scanf ("%d %d %d", &n, &a, &b);

	int sum = 0;

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

		int digitsum = 0;

		int temp = i;

		while (temp != 0){

			digitsum = digitsum + temp % 10;

			temp /= 10;

		}

		if (digitsum >= a && digitsum <= b){

			sum += i;

		}

	}

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

	return 0;

}

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<cstring>

#include<queue>

#define SIZE 1010

#define MOD 998244353



using namespace std;



int n,m;

int ans;

char map[SIZE][SIZE];

int step[SIZE][SIZE];

bool vis[SIZE][SIZE];

struct point

{

    int x,y;

    int steps;

}tmp;

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

bool legal(int x,int y)

{

    if(x==0 || y==0)

        return false;

    if(x>n || y>m)

        return false;

    return !vis[x][y];

}

void bfs()

{

    int i,j;

    queue<point> que;

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

    {

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

        {

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

            {

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

                vis[i][j]=true;

            }

        }

    }

    while(!que.empty())

    {

        tmp=que.front();

        que.pop();

        ans=max(ans,tmp.steps);

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

        {

            int tmpx=tmp.x+dir[i][0];

            int tmpy=tmp.y+dir[i][1];

            if(legal(tmpx,tmpy))

            {

                vis[tmpx][tmpy]=true;

                que.push({tmpx,tmpy,tmp.steps+1});

            }

        }

    }

    return ;

}

int main()

{

    int i,j;

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

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

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

    bfs();

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

    return 0;

}




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



}

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

}


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



}

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

}


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

}

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

}


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

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

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

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

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

	queue<int>q;

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

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

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

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

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

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

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

}

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<queue>

#include<algorithm>

using namespace std;



char c[1001][1002];

int d[1000001];

int main(){

	int n,m;

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

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

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

	queue<int>q;

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

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

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

				q.push((i-1)*m+j);

				d[(i-1)*m+j]=1;

			}

	int ans=0;

	while(!q.empty()){

		int u=q.front();

		q.pop();

		ans=max(ans,d[u]);

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

			int v=u;

			if(i==0&&u<=m||i==1&&u%m==1%m||i==2&&u>=(n-1)*m+1||i==3&&u%m==0)

				continue;

			if(i==0)v-=m;

			else if(i==1)v--;

			else if(i==2)v+=m;

			else v++;

			if(!d[v]){

				d[v]=d[u]+1;

				q.push(v);

			}

		}

	}

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

}

// Fastest known solution for this problem: