importjava.util.Scanner;publicclassMain{staticint[][] graph;staticint[][] direction ={{1,0},{-1,0},{0,1},{0,-1}};publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);int n = sc.nextInt();int m = sc.nextInt();
graph =newint[n][m];for(int i =0; i < n; i++){for(int j =0; j < m; j++){
graph[i][j]= sc.nextInt();}}for(int i =0; i < n; i++){if(graph[i][0]==1)dfs(i,0);if(graph[i][m -1]==1)dfs(i, m -1);}for(int j =0; j < m; j++){if(graph[0][j]==1)dfs(0, j);if(graph[n -1][j]==1)dfs(n -1, j);}for(int i =0; i < n; i++){for(int j =0; j < m; j++){if(graph[i][j]==1) graph[i][j]=0;if(graph[i][j]==2) graph[i][j]=1;}}for(int i =0; i < n; i++){for(int j =0; j < m; j++){System.out.print(graph[i][j]+" ");}}}privatestaticvoiddfs(int i,int j){
graph[i][j]=2;for(int[] dir : direction){int x = i + dir[0];int y = j + dir[1];if(x <0|| x >= graph.length || y <0|| y >= graph[0].length)continue;if(graph[x][y]==1){dfs(x, y);}}}}
importjava.util.Scanner;publicclassMain{staticint[][] direction ={{-1,0},{0,-1},{1,0},{0,1}};staticint[][] graph;// 记录访问情况staticboolean[][][] visited;// 记录可达情况staticboolean[][][] reached;publicstaticvoidmain(String[] args){Scanner sc =newScanner(System.in);int n = sc.nextInt();int m = sc.nextInt();
graph =newint[n][m];// 前两维与graph对应,第三维固定为3,第一组边界修改数组下标为1的位置,第二组边界修改数组下标为2的位置
visited =newboolean[n][m][3];
reached =newboolean[n][m][3];for(int i =0; i < n; i++){for(int j =0; j < m; j++){
graph[i][j]= sc.nextInt();}}for(int i =0; i < n; i++){dfs(i,0,1);}for(int j =0; j < m; j++){dfs(0, j,1);}for(int i =0; i < n; i++){dfs(i, m -1,2);}for(int j =0; j < m; j++){dfs(n -1, j,2);}for(int i =0; i < n; i++){for(int j =0; j < m; j++){if(reached[i][j][1]&& reached[i][j][2]){System.out.println(i +" "+ j);}}}}privatestaticvoiddfs(int i,int j,int mark){
reached[i][j][mark]=true;
visited[i][j][mark]=true;for(int[] dir : direction){int x = i + dir[0];int y = j + dir[1];if(x <0|| y <0|| x >= graph.length || y >= graph[0].length)continue;if(graph[i][j]<= graph[x][y]&&!visited[x][y][mark]){dfs(x, y, mark);}}}}