|
| 1 | +import java.io.*; |
| 2 | +import java.util.*; |
| 3 | + |
| 4 | +public class Main { |
| 5 | + |
| 6 | + static int [][] arr1; |
| 7 | + static int [][] arr2; |
| 8 | + static int N,M; |
| 9 | + public static void main(String[] args) throws Exception { |
| 10 | + |
| 11 | + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + |
| 13 | + StringTokenizer st = new StringTokenizer(br.readLine()); |
| 14 | + |
| 15 | + N = Integer.parseInt(st.nextToken()); |
| 16 | + |
| 17 | + M = Integer.parseInt(st.nextToken()); |
| 18 | + |
| 19 | + arr1 = new int[N][M]; |
| 20 | + |
| 21 | + for(int i =0; i < N; i++) { |
| 22 | + String input = br.readLine(); |
| 23 | + for(int j =0; j<input.length(); j++) { |
| 24 | + arr1[i][j] = Integer.parseInt(input.charAt(j) + ""); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + arr2 = new int[N][M]; |
| 29 | + for(int i =0; i < N; i++) { |
| 30 | + String input = br.readLine(); |
| 31 | + for(int j =0; j<input.length(); j++) { |
| 32 | + arr2[i][j] = Integer.parseInt(input.charAt(j) + ""); |
| 33 | + } |
| 34 | + } |
| 35 | + if(isSameMetric()) { |
| 36 | + System.out.println(0); |
| 37 | + System.exit(0); |
| 38 | + } |
| 39 | + |
| 40 | + if(N < 3 || M <3) { |
| 41 | + System.out.println(-1); |
| 42 | + System.exit(0); |
| 43 | + } |
| 44 | + int count = 0; |
| 45 | + for(int i =0; i<N-2; i++) { |
| 46 | + for(int j =0; j<M-2; j++) { |
| 47 | + if(arr1[i][j] != arr2[i][j]) { |
| 48 | + change(i,j); |
| 49 | + count+=1; |
| 50 | + } |
| 51 | + |
| 52 | + if(isSameMetric()) { |
| 53 | + System.out.println(count); |
| 54 | + System.exit(0); |
| 55 | + } |
| 56 | + |
| 57 | + } |
| 58 | + } |
| 59 | + if(isSameMetric()) { |
| 60 | + System.out.println(count); |
| 61 | + System.exit(0); |
| 62 | + } |
| 63 | + System.out.println(-1); |
| 64 | + } |
| 65 | + |
| 66 | + private static void change(int x, int y) { |
| 67 | + for(int i = x; i< x+3; i++) { |
| 68 | + for(int j = y; j< y+3; j ++) { |
| 69 | + if(arr1[i][j] == 0) { |
| 70 | + arr1[i][j] = 1; |
| 71 | + } else { |
| 72 | + arr1[i][j] = 0; |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private static boolean isSameMetric() { |
| 79 | + for(int i = 0; i< N; i++) { |
| 80 | + for(int j = 0; j< M; j ++) { |
| 81 | + if(arr1[i][j] != arr2[i][j]) { |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + return true; |
| 87 | + } |
| 88 | +} |
0 commit comments