#include <iostream>
using namespace std;


bool pogodno( int n, int x, int y, int **p, bool **b )
{
  int a = p[x][y];
  cout << a << endl;
  int xp = x;
  b[x][y] = true;
  while( p[x + 1][y] == a )
  {
    x++;
    b[x][y] = true;
  }
  int xk = x;
  y++;
  while( y < n )
  {
    x = xp;
    while( x <= xk )
    {
      int yk = y;
      if( p[x][y] != a )
      {
        yk--;
        y = n;
        break;
      }
      b[x][yk] = true;
      x++;
    }
    if( x != xp )
    {
      return false;
    }
    y++;
  }
  int previse = 0;
  for( int l = 0; l < n; l++ )
  {
    for( int m = 0; m < n; m++ )
    {
      if( p[l][m] == a && b[l][m] == false )
      {
        b[l][m] = true;
        previse++;
      }
    }
  }
  if( previse > 0 )
  {
      return false;
  }
  return true;
}

int main()
{
  int n, k;
  cin >> n >> k;
  int p[n][n];
  bool b[n][n];
  for( int m = 0; m < n; m++ )
  {
    for( int l = 0; l < n; l++ )
    {
      cin >> p[m][l];
      b[m][l] = false;
    }
  }
  int brojac = 0;
  int *tempi[n];
  bool *tempb[n];
  for( int i = 0; i < n; i++ )
  {
    tempi[i] = p[i];
    tempb[i] = b[i];
  }
  for( int i = 0; i < n; i++ )
  {
    for( int j = 0; j < n; j++ )
    {
      if( b[i][j] == false )
      {
        bool bo = pogodno( n, i, j, tempi, tempb );
        if( bo == true )
        {
            brojac++;
        }
      }
    }
  }
  cout << brojac << endl;
  return 0;
}