# @param {Integer[][]} mat # @return {Integer} def num_special(mat) count = 0 mat.each_with_index do |row, i| row.each_with_index do |cell, j| count += 1 if cell == 1 && special?(mat, i, j) end end count end def special?(mat, row_index, column_index) mat[row_index].each_with_index do |cell, x| return false if cell == 1 && x != column_index end mat.each_with_index do |row, x| return false if row[column_index] == 1 && x != row_index end true end