1 public class Solution { 2 public int[][] multiply(int[][] A, int[][] B) { 3 int aM = A.length, aN = A[0].length, bN = B[0].length; 4 int [][] result = new int[aM][bN]; 5 for (int i = 0; i < aM; i++) { 6 for (int j = 0; j < aN; j++) { 7 if (A[i][j] != 0) { 8 for (int k = 0; k < bN; k++) { 9 if (B[j][k] != 0) { 10 result[i][k] += A[i][j] * B[j][k]; 11 } 12 } 13 } 14 } 15 } 16 return result; 17 } 18 }