#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct buf {
        int *p;
        int in;
        int out;
};

static void
set(struct buf *buf, int i, int j)
{
        *(buf->p + buf->in * 2) = i;
        *(buf->p + buf->in * 2 + 1) = j;
        buf->in++;
}

static int
get(struct buf *buf, int *i, int *j)
{
        if (buf->in == buf->out)
                return -1;

        *i = *(buf->p + buf->out * 2);
        *j = *(buf->p + buf->out * 2 + 1);
        buf->out++;

        return 0;
}

int
main(void)
{
        int H = 0;
        int W = 0;

        scanf("%d %d\n", &H, &W);

        int *A;
        size_t size;
        size = sizeof(int) * (H + 2) * (W + 2);
        A = malloc(size);
        memset(A, 0, size);
        for (int i = 0; i <= W + 1; i++) {
                A[i] = 1;
                A[(H + 1) * (W + 2) + i] = 1;
        }
        for (int i = 0; i <= H + 1; i++) {
                A[i * (W + 2)] = 1;
                A[i * (W + 2) + W + 1] = 1;
        }

        int white = 0;
        int black = 0;
        struct buf buf;
        size = sizeof(int) * (H) * (W) * 2;
        buf.p = malloc(size);
        buf.in = 0;
        buf.out = 0;
        memset(buf.p, 0, size);

        for (int i = 1; i <= H; i++) {
                for (int j = 1; j <= W; j++) {
                        char c;
                        scanf("%c", &c);
                        if (c == '#') {
                                A[i * (W + 2) + j] = 1;
                                set(&buf, i, j);
                                black++;
                        }
                }
                scanf("\n");
        }

        white = H * W - black;
        int count = 1;
        while(white > 0) {
                int tmp = black;
                black = 0;
                for (int cnt = tmp; cnt > 0; cnt--) {
                        int i;
                        int j;
                        get(&buf, &i, &j);
                        if (A[i * (W + 2) + j] == count) {
                                if (A[(i - 1) * (W + 2) + j] == 0) {
                                        A[(i - 1) * (W + 2) + j] = count + 1;
                                        set(&buf, i - 1, j);
                                        white--;
                                        black++;
                                }
                                if (A[(i + 1) * (W + 2) + j] == 0) {
                                        A[(i + 1) * (W + 2) + j] = count + 1;
                                        set(&buf, i + 1, j);
                                        white--;
                                        black++;
                                }
                                if (A[i * (W + 2) + (j - 1)] == 0) {
                                        A[i * (W + 2) + (j - 1)] = count + 1;
                                        set(&buf, i, j - 1);
                                        white--;
                                        black++;
                                }
                                if (A[i * (W + 2) + (j + 1)] == 0) {
                                        A[i * (W + 2) + (j + 1)] = count + 1;
                                        set(&buf, i, j + 1);
                                        white--;
                                        black++;
                                }
                        }
                }
                count++;
        }

        printf("%d", count - 1);
}


Measured memory_bytes: 11904