Java DP solution
Store dp state and add to count in same nested for loop.\nDP state to store: find min of (i-1,j), (i,j-1), (i-1,j-1) and add one.  \n\tfor example:\n\t\t\t1 1\n
Store dp state and add to count in same nested for loop.\nDP state to store: find min of (i-1,j), (i,j-1), (i-1,j-1) and add one.  \n\tfor example:\n\t\t\t1 1\n\t\t\t1 1 <---this should be 2 because it\'s both a 2x2 square and a 1x1 square\n\tanother bigger example:\n\t\nBEFORE\n   [0,1,1,1]\n   [1,1,1,1]\n   [0,1,1,1]\nAFTER\n   [0,1,1,1]\n   [1,1,2,2]\n   [0,1,2,3]\n\n\n```\nclass Solution {\n    public int countSquares(int[][] matrix) {\n        int count = 0;\n        \n        for (int i=0;i<matrix.length;i++) {\n            for (int j=0;j<matrix[0].length;j++) {\n                if (i != 0 && j != 0 && matrix[i][j] != 0) {\n                    int min = Math.min(Math.min(matrix[i-1][j-1], matrix[i-1][j]), matrix[i][j-1]);\n                    matrix[i][j] = min + 1;\n                }\n                count += matrix[i][j];\n            }\n        }\n        \n        return count;\n    }\n}\n```
[]