Kotlin prefix sum
Code\n\nclass Solution {\n    fun largest1BorderedSquare(g: Array<IntArray>): Int {\n        var a = Array<Array<IntArray>>(g.size, { Array(g[0].size, { IntArra
# Code\n```\nclass Solution {\n    fun largest1BorderedSquare(g: Array<IntArray>): Int {\n        var a = Array<Array<IntArray>>(g.size, { Array(g[0].size, { IntArray(4, { 0 }) }) })\n\n        for (r in 0..g.size - 1) {\n            var t = a[r]\n\n            var z = 0\n            for (c in 0..g[0].size - 1) {\n                if (g[r][c] == 1) {\n                    z++\n                    t[c][0] = z\n                } else z = 0\n            }\n            z = 0\n            for (c in g[0].size - 1 downTo 0) {\n                if (g[r][c] == 1) {\n                    z++\n                    t[c][1] = z\n                } else z = 0\n            }\n        }\n\n        for (c in 0..g[0].size - 1) {\n            var z = 0\n            for (r in 0..g.size - 1) {\n                if (g[r][c] == 1) {\n                    z++\n                    a[r][c][2] = z\n                } else z = 0\n            }\n\n            z = 0\n            for (r in g.size - 1 downTo 0) {\n                if (g[r][c] == 1) {\n                    z++\n                    a[r][c][3] = z\n                } else z = 0\n            }\n        }\n\n        var res = 0\n\n        for (r in 0..g.size - 1) {\n            for (c in 0..g[0].size - 1) {\n                if (g[r][c] == 1) {\n                    var m = Math.min(a[r][c][1], a[r][c][3])\n                    if (m <= res) continue\n                    \n                    var diff = m - (res + 1)\n\n                    // check all from m downTo current res + 1\n                    for (k in 0..diff) {\n                        var row = r + (m - 1 - k)\n                        var col = c + (m - 1 - k)\n                        if (a[row][col][0] >= m - k && a[row][col][2] >= m - k) {\n                            res = m - k\n                            break\n                        }\n                    }\n                }\n            }\n        }\n\n        return res * res\n    }\n}\n```
['Prefix Sum', 'Kotlin']