Это решение в C ++. ^^
// Example program
#include <iostream>
#include <string>
using namespace std;
int main()
{
int m[5][5]={1, 2, 3, 4,5,
6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
16, 17, 18, 19, 20,
21, 22, 23, 24, 25};
int n = 2; //number of cells you want to "move" from the origin
int k=2;//for a cell in m[k][k]
for(int i=1;i<=n;i++) //this is for the size of the "square" you form around the initial cell
{
cout<<i<<endl;
for(int j=i;j>=-i;j--)
{
cout<<m[k-i][k-j]<<" ";//cell of the first line, on top of the new formed "square"
}
cout<<endl;
for(int j=-i+1;j<=i-1;j++)
{
cout<< m[k+j][k-i]<<" ";//on the left side of the "square"
cout<< m[k+j][k+i]<<endl;//on the right side of the "square"
}
for(int j=i;j>=-i;j--)
{
cout<<m[k+i][k-j]<<" ";//cell of the first line, on the bottom of the new formed "square"
}
cout<<endl;
}
}
/**Result:
1
7 8 9
12 14
17 18 19
2
1 2 3 4 5
6 10
11 15
16 20
21 22 23 24 25 */