I have a matrix of 0 and 1

```
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 1 1 0 1 1 1 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1
0 0 1 1 1 1 1 0 0 0
0 0 0 0 1 1 0 1 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 1 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 1 0 0 0 0

```

I want to calculate the shortest distance of each item from the non-empty edges. You can assume it as pixels of black and white (or empty). We want to calculate the distance of each black pixel from the edge of the black body (through a straight line; horizontal, vertical or diagonal).

The distance is calculated geometrically: the distance of Ai,j and Ai+1,j+1 is `sqrt(2)`.

With the following code in C, I started to calculate the distance of each item from its horizontal and vertical edges (in four directions). Now I want to consider diagonal distance too (at any angle, not just 45°).

The straightforward approach is to calculate the distance of each item from each edge. However, as the matrix gets larger, the loops become painfully slow.

Since we have the distance from horizontal and vertical edges for each item, I look for a hack to find the shortest distance to the edge by examining neighbour items in fewer checks.

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

int min(int a, int b, int c, int d)
{
    int e = a < b ? a : b;
    int f = c < d ? c : d;
    int r = e < f ? e : f;
    return r;
}

int main()
{

    int width = 50;
    int height = 50;
    int points[width][height];
    int distances[width][height][5]; // 0 left 1 right 2 bottom 3 top 4 min

    // some random data
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            points[x][y] = rand() % 2;
        }
    }

    // scanning in four direction to check if the previous neighbour exists
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (points[x][y] > 0)
            {
                if (x > 0)
                {
                    distances[x][y][0] = distances[x - 1][y][0] > 0 ? distances[x - 1][y][0] + 1 : 1;
                }
                else
                {
                    distances[x][y][0] = 1;
                }
            }
        }
        for (int x = width - 1; x >= 0; x--)
        {
            if (points[x][y] > 0)
            {
                if (x < width - 1)
                {
                    distances[x][y][1] = distances[x + 1][y][1] > 0 ? distances[x + 1][y][1] + 1 : 1;
                }
                else
                {
                    distances[x][y][1] = 1;
                }
            }
        }
    }

    for (int x = 0; x < width; x++)
    {
        for (int y = 0; y < height; y++)
        {
            if (points[x][y] > 0)
            {
                if (y > 0)
                {
                    distances[x][y][2] = distances[x][y - 1][2] > 0 ? distances[x][y - 1][2] + 1 : 1;
                }
                else
                {
                    distances[x][y][2] = 1;
                }
            }
        }
        for (int y = height - 1; y >= 0; y--)
        {
            if (points[x][y] > 0)
            {
                if (y < height - 1)
                {
                    distances[x][y][3] = distances[x][y + 1][3] > 0 ? distances[x][y + 1][3] + 1 : 1;
                }
                else
                {
                    distances[x][y][3] = 1;
                }
            }
        }
    }

    // finding the minimum of four distances
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            if (points[x][y] > 0)
            {
                distances[x][y][4] = min(distances[x][y][0], distances[x][y][1], distances[x][y][2], distances[x][y][3]);
                printf("%d %d %d %d %d %d %d \n", x, y, distances[x][y][0], distances[x][y][1], distances[x][y][2], distances[x][y][3], distances[x][y][4]);
            }
        }
    }

    return 0;
}

```