fork download
  1. import java.util.*;
  2.  
  3. public class Main{
  4. public static int ram(int[][] a){
  5. int c=0;
  6. int rl = a.length;
  7. int cl = a[0].length;
  8. for(int i=0;i<rl;i++){
  9. for(int j=0;j<cl;j++){
  10. if(a[i][j] == 1){
  11. if(i-1 == -1 || a[i-1][j] == 0){
  12. c++;
  13. }
  14. if(j-1 == -1 || a[i][j-1] == 0){
  15. c++;
  16. }
  17. if(i+1 == rl || a[i+1][j] == 0){
  18. c++;
  19. }
  20. if(j+1 == cl || a[i][j+1] == 0){
  21. c++;
  22. }
  23. }
  24. }
  25. }
  26. return c;
  27. }
  28.  
  29. public static void main(String[] args){
  30. Scanner sc = new Scanner(System.in);
  31. int rt = sc.nextInt();
  32. while(rt>0){
  33. int r = sc.nextInt();
  34. int c = sc.nextInt();
  35. int[][] a = new int[r][c];
  36. for(int i=0;i<r;i++){
  37. for(int j=0;j<c;j++){
  38. a[i][j] = sc.nextInt();
  39. }
  40. }
  41. System.out.println(ram(a));
  42.  
  43. rt--;
  44. }
  45. }
  46. }
Success #stdin #stdout 0.14s 56620KB
stdin
1
4 4
0 1 0 0
1 1 1 0
0 1 0 0
1 1 1 0
stdout
18