Create a matrix with all elements having the value of the shortest path to a given position
Some suggestions:


Use variable names that tells the reader what they are.
Dont do using namespace std; in the global namespace.
Always check that &lt;stream&gt; &gt;&gt; variable actually worked or else your program will run with uninitialized variables and cause undefined behaviour if they are read.
Use an unsigned type when dealing with subscripting.
If you use a hardcoded array size, check that the values entered by the user actually fits in the array.
Don't use a hardcoded array size when the required size is unknown at compile time.
Use 0-based arrays instead of 1-based.


When it comes to the actual algorithm, it seems like that the shortest path from any point is
 abs(point.row - reference.row) + abs(point.column - reference.column)
which would give code like this:

#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;
#include &lt;vector&gt;

// a helper type to keep the reference point
class point_t {
public:
    point_t() : point_t(0, 0) {}
    point_t(unsigned Row, unsigned Col) : row(Row), col(Col) {}

    // a function to calculate the distance to another point
    unsigned distance_to(const point_t&amp; p) const {
        return static_cast&lt;unsigned&gt;(
            std::abs(static_cast&lt;int&gt;(row) - static_cast&lt;int&gt;(p.row)) +
            std::abs(static_cast&lt;int&gt;(col) - static_cast&lt;int&gt;(p.col)));
    }

    // reading the point from an istream
    friend std::istream&amp; operator&gt;&gt;(std::istream&amp; is, point_t&amp; p) {
        return is &gt;&gt; p.row &gt;&gt; p.col;
    }

private:
    unsigned row;
    unsigned col;
};

int main() {
    unsigned rows;
    unsigned cols;
    point_t ref;

    if(std::cin &gt;&gt; rows &gt;&gt; cols &gt;&gt; ref) {
        // check that it's not too big
        if(rows &gt; std::numeric_limits&lt;int&gt;::max() ||
           cols &gt; std::numeric_limits&lt;int&gt;::max()) {
            std::cerr &lt;&lt; "matrix too big\n";
            return 1;
        }

        // create a 2D vector
        std::vector&lt;std::vector&lt;unsigned&gt;&gt; mat(rows, std::vector&lt;unsigned&gt;(cols, 0));

        // fill the matrix
        for(unsigned row = 0; row &lt; rows; ++row) {
            for(unsigned col = 0; col &lt; cols; ++col) {
                // use the distance_to function
                mat[row][col] = ref.distance_to(point_t(row, col));
            }
        }

        // print the result
        for(const auto&amp; row : mat) {
            for(auto col : row) std::cout &lt;&lt; col;
            std::cout &lt;&lt; '\n';
        }
    }
}


Input (with a 0-based reference point)

3 4 2 2


Output

4323
3212
2101