title: Setting Base Cases for Recursive Function
body: <pre><code>class Solution {
  
  //given a location on the matrix, this function recursively find the deepest possible depth, which is the length of a side of a found square
  private int isSquare(int row_index, int col_index, int depth, char[][] matrix) {
      
      int last_row = row_index + depth;
      int last_col = col_index + depth;
      
      if (row_index &gt;= matrix.length || col_index &gt;= matrix[0].length) {
          return 0;
      }
      if (last_row &gt;= matrix.length || last_col &gt;= matrix[0].length) {
          return 0;
      }
      
  
      for (int i = col_index; i &lt; last_col; i++) {
          
          if (matrix[row_index][i] != '1') {
              return 0;
          }
      }
      
      for (int i = row_index; i &lt; last_row; i++ ) {
          
          if (matrix[i][col_index] != '1') {
              return 0;
          }
      }
      
      return Math.max(depth, isSquare(row_index, col_index, depth + 1, matrix));
  }
  
  public int maximalSquare(char[][] matrix) {
      
      int max = 0;
      
      for (int row = 0; row &lt; matrix.length; row ++) {
          for (int col = 0; col &lt;matrix[0].length; col ++) {
              
              int curr_depth = isSquare(row, col, 1, matrix);
              
              if (curr_depth &gt; max) {
                  max = curr_depth;
              }
              
          }
      };
      
      return max * max;
  }
  </code></pre>
  <p>}</p>
  <p>Hi, I was working on LeetCode 221, and it seems like that my solution is not passing test cases with output 1, where the biggest square on the given matrix is just 1 x 1. To me it looks like those depth 1 cases are not passing the two for loops in function isSquare, which is supposed to catch 0s in the square.</p>
  <p>I tried LC debugging tool but it did not help much, and my base cases seem fine to me. Please let me know what is going on here. For the problem, <a href="https://leetcode.com/problems/maximal-square/" rel="nofollow noreferrer">https://leetcode.com/problems/maximal-square/</a></p>
  <p>One of the depth 1 test cases that I am failing is below.</p>
  <p>Input:
  [[&quot;0&quot;,&quot;1&quot;],[&quot;1&quot;,&quot;0&quot;]]
  Output:
  0
  Expected:
  1</p>