Well the first thing is to learn the value of indentation.

SourceForge.net: Indentation - cpwiki

Here is your code, indented.

Code:

int a[4][4] = { {1, 2, 3, 4}, //i mor
{5, 6, 7, 8}, //j bagana
{9, 10, 11, 12},
{13, 14, 15, 16}
};
int i1;
int j1;
int i = 0, j = 0, b[5][5], r, chose, sanakh = 4, q, too = 0, ber =
0, save = 0, horg = 3, qwe = 0, ewq = 0;
int sum[1000];
for (int l = 0; l < 1000; l++)
sum[l] = 0;
// while(1==1){
// for(j=0;j<=3;j++)
// {
for (r = j; r <= 3; r++) {
for (q = 0; q <= 16; q++) {
save = 0;
top:
for (i = 0; i <= 3; i++) {
if (i < horg) {
save = save + a[i][r];
}
if (horg == i && qwe == ewq) {
for (int z = 0; z <= 3; z++) {
sum[ber] = save + a[i][z];
ber++;
too++;
qwe++;
}
save = 0;
horg--;
} else if (horg == i) {
sanakh = save;
for (int t = r + 1; t <= 3; t++) {
sanakh = save + a[i][t];
for (int d = 0; d <= 3; d++) {
cout << sanakh << " ";
sum[ber] = a[i + 1][d] + sanakh;
ber++;
too++;
}
}
horg--;
if (i < 4) {
goto top;
}
}
}
sanakh--;
}
}
int h = 1;
for (q = 0; q <= 30; q++) {
cout << sum[q] << " ";
if (h == 4) {
cout << endl;
h = 0;
}
h++;
}

next_permutation

Create an array called

Code:

row_index[4] = { 0, 1, 2, 3 };

Create another called

Code:

col_index[4] = { 0, 1, 2, 3 };

For each permutation of row_index, you permute all of col_index

For all of that, you do

Code:

for ( i = 0 ; i < 4 ; i++ )
sum += a[row_index[i]][col_index[i]];