package com.itranswarp.learnjava;
import java.util.*;
public class test2 {
static int [][] biaoji=new int[100][100];
static int [][] intA=new int[100][100];
static List list1=new ArrayList<>();
public static void main(String[] args){
Scanner cin = new Scanner(System.in);
int m, n;
while(true) {
list1.clear();
String[] stre=cin.nextLine().split(" ");
m = Integer.parseInt(stre[0]);
n = Integer.parseInt(stre[1]);
if (m == -1 && n == -1) {
break;
}
for (int i = 0; i
String[] arr = cin.nextLine().split(" ");
for (int j = 0; j
intA[i][j] = Integer.parseInt(arr[j]);
}
}
for (int i = 0; i
for (int j = 0; j
biaoji[i][j] = 0;
}
}
int fangxiang = 0;
test(0, 0, 0,m,n);
String sss=String.join(",",list1);
System.out.println(sss);
}
}
public static boolean isall(int[][] aa,int m,int n){
int num=0;
for(int i=0;i
for(int j=0;j
if(aa[i][j]==0){
num=1;
break;
}
}
}
if(num==1){
return true;
}
else {
return false;
}
}
public static void test(int x,int y,int fangxiang,int m,int n){
if(fangxiang==0){
if(x!=0 || y!=0 ){ y++;}
while ( y
list1.add(Integer.toString(intA[x][y]));
biaoji[x][y]=1;
y++;
}
if(isall(biaoji,m,n)){
test(x, y-1, 1,m,n);
}
}
else if(fangxiang==1){
x++;
while(x
list1.add(Integer.toString(intA[x][y]));
biaoji[x][y]=1;
x++;
}
if(isall(biaoji,m,n)){
test(x-1, y, 2,m,n);
}
}
else if(fangxiang==2){
x--;
while ( x>=0 && biaoji[x][y]==0 ){
list1.add(Integer.toString(intA[x][y]));
biaoji[x][y]=1;
x--;
}
if(isall(biaoji,m,n)){
test(x+1, y, 3,m,n);
}
}
else if(fangxiang==3){
y--;
while ( y>=0 && biaoji[x][y]==0 ){
list1.add(Integer.toString(intA[x][y]));
biaoji[x][y]=1;
y--;
}
if(isall(biaoji,m,n)){
test(x, y+1, 0,m,n);
}
}
}
}