Language: C++

#include<bits/stdc++.h>
using namespace std;

#define NAN 10000000

// bool isblack(int h, int w, int H, int W, std::vector<string> &table){
//     if(h < 0 || h > H - 1 || w < 0 || w > W - 1){
//         return false;
//     }else if(table.at(h).at(w) == '#'){
//         return true;
//     }
//     return false;
// }
//
// bool isAdj(int h, int w, int H, int W, std::vector<string> &table){
//     return isblack(h - 1, w , H, W, table) || isblack(h+1, w , H, W, table) || isblack(h, w-1 , H, W, table) || isblack(h, w+1 , H, W, table);
// }
//
// int caldist(int h, int w, int H, int W, std::vector<string> &table, std::vector<std::vector<int> > &dist){
//     if(h < 0 || h > H - 1 || w < 0 || w > W - 1){
//         return NAN;
//     }
//     if(dist.at(h).at(w) < NAN){
//         return dist.at(h).at(w);
//     }
//     if (isblack(h, w, H, W, table))
//     {
//         dist.at(h).at(w) == 0;
//         return 0;
//     }
//     int mindis = NAN;
//     mindis = min(mindis, caldist(h-1, w, H, W, table, dist));
//     mindis = min(mindis, caldist(h, w-1, H, W, table, dist));
//     mindis = min(mindis, caldist(h+1, w, H, W, table, dist));
//     mindis = min(mindis, caldist(h, w+1, H, W, table, dist));
//     dist.at(h).at(w) = mindis;
//     return mindis;
//
//
//
// }

int update(std::vector<std::vector<int> > &dist, int counter, int H, int W){
    bool flag = false;
    for(int i=0; i < H; i++){
        for(int j=0; j < W; j++){
            if(dist.at(i).at(j) == counter){
                flag = true;
                if(i > 0){
                    dist.at(i-1).at(j) = min(dist.at(i-1).at(j), counter + 1);
                }
                if(i < H - 1){
                    dist.at(i+1).at(j) = min(dist.at(i+1).at(j),counter + 1);
                }
                if(j > 0){
                    dist.at(i).at(j-1) = min(dist.at(i).at(j-1),counter + 1);
                }
                if(j < W - 1){
                    dist.at(i).at(j + 1) = min(dist.at(i).at(j+1),counter + 1);
                }
            }
        }
    }
    return flag;
}

int main(){
    int H, W;
    cin >> H >> W;
    std::vector<string> table(H);
    std::vector<std::vector<int> > dist(H, std::vector<int>(W, NAN));
    for(int i=0; i < H; i++){
        cin >> table.at(i);
    }
    for(int i=0; i < H; i++){
        for(int j=0; j < W; j++){
            if(isblack(i,j, H, W, table)){
                dist.at(i).at(j) = 0;
            }
        }
    }
    int counter = 0;
    int prev = 0;
    while (1)
    {
        prev = counter;
        counter += update(dist, counter, H, W);
        if (prev == counter) break;
    }
    // for(int i=0; i < H; i++){
    //     for(int j=0; j < W; j++){
    //         cout << dist.at(i).at(j) << " ";
    //     }
    //     cout << endl;
    // }
    cout << counter -1 << endl;

    return 0;
}


Compiler output:
a.cc:4:9: warning: "NAN" redefined
    4 | #define NAN 10000000
      |         ^~~
In file included from /usr/include/c++/14/cmath:47,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:114,
                 from a.cc:1:
/usr/include/math.h:101:12: note: this is the location of the previous definition
  101 | #   define NAN (__builtin_nanf (""))
      |            ^~~
a.cc: In function 'int main()':
a.cc:77:16: error: 'isblack' was not declared in this scope; did you mean 'isblank'?
   77 |             if(isblack(i,j, H, W, table)){
      |                ^~~~~~~
      |                isblank